Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash Uniq vs Javascript Set Iterator vs Javascript Set Array.from
(version: 0)
Comparing performance of:
Lodash Uniq vs Javascript Set Iterator vs Javascript Set Array.from
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
Script Preparation code:
var firstEqual = Array.from({length: 1000000}, (item, idx) => idx); var secondEqual = Array.from({length: 1000000}, (item, idx) => idx); var arrayToDedup = firstEqual.concat(secondEqual);
Tests:
Lodash Uniq
_.uniq(arrayToDedup);
Javascript Set Iterator
[...new Set(arrayToDedup)]
Javascript Set Array.from
Array.from(new Set(arrayToDedup))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash Uniq
Javascript Set Iterator
Javascript Set Array.from
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares three approaches to remove duplicates from an array of 1,000,000 elements: 1. **Lodash Uniq**: A function from the Lodash library that returns a new array with unique values. 2. **Javascript Set Iterator**: Using the `Set` data structure and its iterator methods to remove duplicates. 3. **Javascript Set Array.from**: Using the `Set` data structure and the `Array.from()` method to create an array with unique values. **Options Comparison** The benchmark tests these three approaches by generating a large array of random numbers (`arrayToDedup`) and then applying each approach to it. Here's a brief summary of each approach: * **Lodash Uniq**: This function takes an array as input and returns a new array with unique values. It uses a `Map` data structure internally to keep track of the unique elements. * **Javascript Set Iterator**: This approach uses the `Set` data structure, which automatically removes duplicates. The test creates a set from the input array using `new Set(arrayToDedup)`, and then converts it back to an array using `Array.from()`. However, since sets are unordered, this approach may not be suitable for all use cases. * **Javascript Set Array.from**: This approach is similar to the previous one, but uses the `Array.from()` method instead of converting the set directly. This approach can be slightly more efficient than the iterator approach because it avoids the overhead of creating a new array. **Pros and Cons** Here are some pros and cons for each approach: * **Lodash Uniq**: Pros: Efficient, stable, and widely used. Cons: Adds an external dependency (Lodash), may not perform well with very large inputs. * **Javascript Set Iterator**: Pros: Fast, efficient, and uses built-in data structures. Cons: May not be suitable for ordered arrays, can be slow due to set creation and conversion overhead. * **Javascript Set Array.from**: Pros: Similar efficiency to the iterator approach, but avoids the conversion overhead. Cons: May still be slower than Lodash Uniq due to set creation. **Library and Special JS Features** The benchmark uses the following library: * **Lodash**: A popular utility library for JavaScript that provides a wide range of functions for common tasks, including array manipulation. No special JS features or syntax are used in this benchmark. It only relies on standard JavaScript data structures and methods. **Alternatives** If you're interested in alternative approaches to removing duplicates from an array, here are a few options: * **Using `filter()`**: You can use the `filter()` method to create a new array with unique values: `arrayToDedup.filter((item, idx) => arrayToDedup.indexOf(item) === idx);` * **Using `reduce()`**: You can use the `reduce()` method to create an object and then convert it back to an array with unique values: `{[item]: (obj[item] || []).push(item), obj} = arrayToDedup.reduce((obj, item) => ({[item]: (obj[item] || []).push(item), obj}), {});` * **Using a custom implementation**: You can also implement a custom function to remove duplicates from an array using a simple algorithm, such as sorting the array and then iterating over it. Keep in mind that these alternatives may have different performance characteristics compared to the benchmarked approaches.
Related benchmarks:
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
lodash uniq vs set - 3
Lodash union vs Native Javascript
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?