Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique elements in array comparing 10000 item arrays
(version: 0)
Comparing performance of:
_.uniq vs set with spread vs uniq by filter vs set with array.from
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Script Preparation code:
var firstEqual = []; var secondEqual = []; for (var i=0; i<=10000; i++) { firstEqual.push(i); secondEqual.push(i); } var elements = [...firstEqual, ...secondEqual];
Tests:
_.uniq
_.uniq(elements)
set with spread
[...new Set(elements)]
uniq by filter
elements.filter((v, i, a) => a.indexOf(v) === i)
set with array.from
Array.from(new Set(elements))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
_.uniq
set with spread
uniq by filter
set with 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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The provided benchmark measures the performance of different methods to find unique elements in an array of 10,000 items. The script preparation code creates two arrays, `firstEqual` and `secondEqual`, each populated with 10,000 consecutive integers from 0 to 9,999. These arrays are then merged into a single array called `elements`. **Comparison of Methods** The benchmark compares four methods: 1. **Lodash's `_.uniq`**: This method uses the Lodash library to find unique elements in the array. 2. **Set with spread (`[...new Set(elements)]`)**: This method converts the array into a set and then spreads its elements back into an array, effectively removing duplicates. 3. **Filtering by index (`elements.filter((v, i, a) => a.indexOf(v) === i)`)**: This method iterates over the array, filtering out elements that have already been encountered (i.e., those with duplicate indices). 4. **Array.from(new Set(elements))**: Similar to the previous method, but uses `Array.from()` to create an array from a set. **Pros and Cons of Each Method** 1. **Lodash's `_._uniq`**: * Pros: Highly optimized, efficient, and tested by Lodash maintainers. * Cons: Requires an external library dependency, which may impact performance for smaller codebases. 2. **Set with spread (`[...new Set(elements)]`)**: * Pros: Fast, concise, and does not require additional dependencies. * Cons: Creates a new set, which can be memory-intensive, and might incur higher overhead due to the creation of an intermediate data structure. 3. **Filtering by index (`elements.filter((v, i, a) => a.indexOf(v) === i)`)**: * Pros: No additional dependencies required, and can take advantage of browser optimizations for array indexing. * Cons: May have higher performance overhead due to the filtering process and potential issues with duplicate indices. 4. **Array.from(new Set(elements))**: * Pros: Similar to the previous method, but avoids using `filter()` which might incur additional overhead. * Cons: Same memory-intensive concerns as the set-based approach. **Other Considerations** When choosing a method for finding unique elements in an array, consider factors such as: * Code readability and maintainability * Performance requirements (e.g., real-time applications, large datasets) * Memory constraints (especially when working with sparse or large data sets) In general, methods like Lodash's `_._uniq` and Set-based approaches are well-suited for most use cases due to their performance and conciseness. However, filtering by index might be a better choice if memory is a concern, while Array.from(new Set(elements)) provides a middle ground between the two. **Library: Lodash** Lodash is a popular JavaScript library that provides a wide range of utility functions for data manipulation, string manipulation, and more. The `_._uniq` method is one of its many optimizations for finding unique elements in an array. **JavaScript Feature/ Syntax: None** There are no special JavaScript features or syntax used in this benchmark, making it accessible to developers with varying levels of expertise in the language. I hope this explanation helps you understand the provided benchmark and the methods being compared!
Related benchmarks:
lodash uniq vs vanilla set
Lodash Uniq vs Javascript Set Iterator vs Javascript Set Array.from
unique elements in array using filter with 20000 items
Lodash Uniq vs Javascript Set 10000 items
Comments
Confirm delete:
Do you really want to delete benchmark?