Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash Uniq vs ES Set
(version: 0)
Comparing performance of:
Lodash Uniq vs Javascript Set Iterator vs Javascript Set Array.from
Created:
6 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/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):
This benchmark is designed to compare the performance of three approaches for removing duplicates from an array: Lodash's `uniq` function, JavaScript's built-in `Set` iterator, and `Array.from(new Set(arrayToDedup))`. **Lodash's `uniq` function** The `uniq` function returns a new array with all duplicate elements removed. The provided script preparation code creates two arrays, `firstEqual` and `secondEqual`, both containing one million elements (indexed from 0 to 999,999). These arrays are then concatenated together to create the `arrayToDedup` array. Lodash's `uniq` function is a popular utility function in the JavaScript ecosystem. It takes an array as input, removes duplicate elements, and returns a new array with unique elements only. The function uses a technique called "short-circuiting" to quickly identify duplicates, which makes it efficient for large datasets. **JavaScript's built-in `Set` iterator** The second approach uses JavaScript's built-in `Set` data structure to remove duplicates. A `Set` is an unordered collection of unique values, and the provided script preparation code creates a new set from the concatenated `arrayToDedup` array using the spread operator (`[...new Set(arrayToDedup)]`). The `Set` iterator works by creating a new set from the input array. When iterating over this set, JavaScript automatically skips duplicates because sets only store unique values. This approach is concise and efficient but relies on JavaScript's built-in set implementation. **Array.from(new Set(arrayToDedup))** This third approach uses the `Array.from()` method to create a new array from the result of creating a new set from the concatenated `arrayToDedup` array (`Array.from(new Set(arrayToDedup))`). This approach is similar to using the built-in `Set` iterator but explicitly creates an array and then converts it to a set. Both approaches are equivalent in terms of performance, as they both use JavaScript's optimized implementation for sets. However, the first approach, Lodash's `uniq`, may be slightly faster due to its specialized implementation and short-circuiting algorithm. **Considerations** * For large datasets, using a library like Lodash or implementing a custom set iterator can be more efficient than relying on built-in JavaScript methods. * The use of arrays with millions of elements is unlikely in most real-world scenarios. This benchmark simulates such a case to demonstrate performance differences. * Browser and environment factors (e.g., Chrome version, device platform) can impact the results, as shown by the provided benchmark result. **Other alternatives** Alternative approaches could include: 1. Using `Filter()` method: `arrayToDedup.filter((item, idx, arr) => arr.indexOf(item) === idx)` 2. Implementing a custom set iterator using recursive functions or loops 3. Utilizing third-party libraries like `fast-set` or `lodash-es` Keep in mind that these alternatives may have different performance characteristics and may not be as efficient as the approaches used in this benchmark. As a software engineer, understanding the trade-offs between different approaches to solving problems can help you choose the most suitable solution for your specific use case.
Related benchmarks:
Lodash Uniq vs Javascript Set Iterator vs Javascript Set Array.from
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
lodash uniqBy vs custom uniqBy
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?