Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique elements in array using filter v2.3
(version: 0)
Comparing performance of:
_.uniq vs set vs uniq by filter
Created:
4 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 elements = [8,9,34,12,4,2,1,5,7,2,8,1,25,7,1,2,3,1,2,4,2,3,5,3,9,34,12,4,2,1,5,7,2,8,1,25,7,1,2,3,1,2,4,2,39,34,12,4,2,1,5,7,2,8,1,25,7,1,2,3,1,2,4,2,39,34,12,4,2,1,5,7,2,8,1,25,7,1,2,3,1,2,4,2,39,34,12,4,2,1,5,7,2,8,1,25,7,1,2,3,1,2,4,2,39,34,12,4,2,1,5,7,2,8,1,25,7,1,2,3,1,2,4,2,39,34,12,4,2,1,5,7,2,8,1,25,7,1,2,3,1,2,4,2,3,7,2,8,1,25,7,1,2,3,1,2,4,2,37,2,8,1,25,7,1,2,3,1,2,4,2,37,2,8,1,25,7,1,2,3,1,2,4,2,37,2,8,1,25,7,1,2,3,1,2,4,2,3]
Tests:
_.uniq
_.uniq(elements)
set
[...new Set(elements)]
uniq by filter
elements.filter((v, i, a) => a.indexOf(v) === i)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.uniq
set
uniq by filter
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):
Measuring JavaScript performance is an essential task in software development, and MeasuringThat.net provides a useful platform for it. The provided benchmark tests the uniqueness of elements in an array using three different approaches: 1. **_.uniq(elements)** (using Lodash library): * **Approach:** This approach uses the `uniq` function from the Lodash library, which is a utility function that returns a new array with unique values. * **Pros:** The Lodash library provides a simple and efficient way to perform this operation. It's also well-documented and widely used in the JavaScript community. * **Cons:** Since it uses an external library, there might be slight overhead due to the initialization of the library. 2. **[...new Set(elements)]**: * **Approach:** This approach creates a new `Set` object from the input array and then converts it back to an array using the spread operator (`[...]`). The `Set` data structure automatically removes duplicates, making this approach efficient. * **Pros:** This approach is fast and simple, with an average time complexity of O(n). It's also a native JavaScript solution, eliminating any external library dependencies. * **Cons:** Creating a new `Set` object can be memory-intensive for large arrays. Additionally, this approach may not work as expected if the input array contains non-unique elements that are considered equal (e.g., due to a custom equality check). 3. **elements.filter((v, i, a) => a.indexOf(v) === i)**: * **Approach:** This approach uses the `filter` method to create a new array with only unique elements. It achieves this by comparing each element's index in the original array (`a.indexOf(v)` ) to its value (`v`). If they're equal, it means the element is unique. * **Pros:** This approach is also efficient (O(n)) and uses native JavaScript functions, making it a good choice for performance-critical code paths. * **Cons:** It may be slightly slower than the `Set` approach due to the extra function calls involved. All three approaches have their strengths and weaknesses. The Lodash library's `uniq` function is convenient but might introduce some overhead. The `Set` approach is fast and simple, but it requires memory for the temporary set data structure. The filter-based approach is another efficient option with native JavaScript functions, but it may require more complex logic. **Additional considerations:** * When using external libraries like Lodash, ensure that they're up-to-date and compatible with your target browsers. * For very large arrays or performance-critical code paths, consider the memory requirements of each approach. * Be aware of potential edge cases for unique elements, such as custom equality checks. **Other alternatives:** * If you want to implement a custom solution without relying on external libraries, you can use a simple hash table-based approach (e.g., by creating an object with keys equal to the input array values). * Another option is to use the `Array.prototype.reduce()` method along with a custom equality check function to remove duplicates.
Related benchmarks:
using .length within and out of for loop
unique elements in array using filter v2
unique elements in array using filter - large array
set.has vs. array.includes bigger sample
Comments
Confirm delete:
Do you really want to delete benchmark?