Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique elements in array using filter
(version: 1)
Comparing performance of:
_.uniq vs set vs uniq by filter
Created:
7 years ago
by:
Registered User
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 = [1,2,3,1,2,4,2,3,5,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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Whale/3.26.244.21 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.uniq
3442279.2 Ops/sec
set
2795562.0 Ops/sec
uniq by filter
6238017.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. The benchmark measures the performance of three different approaches to find unique elements in an array: 1. **_.uniq(elements)**: This is a function from the Lodash library, which is a popular utility library for JavaScript. The `_` notation refers to the Lodash root namespace. The `uniq` function takes an array as input and returns a new array with only unique elements. 2. **[...new Set(elements)]**: This approach uses the built-in `Set` object in JavaScript. When you create a new `Set` from an array, it automatically removes duplicates because sets only store unique values. By spreading the original array into a new `Set`, we can get a new array with unique elements. 3. **elements.filter((v, i, a) => a.indexOf(v) === i)**: This approach uses the `filter()` method to create a new array with only unique elements. It works by filtering out elements that are already present in their original index position (`i`). The callback function checks if the element is at its first occurrence (by comparing its index with its value's index in the array). Now, let's discuss the pros and cons of each approach: * **_.uniq(elements)**: + Pros: High performance and conciseness. Lodash provides an optimized implementation that handles edge cases efficiently. + Cons: It relies on an external library (Lodash), which might add extra overhead. * **[...new Set(elements)]**: + Pros: Fast, efficient, and easy to understand. It leverages the built-in `Set` object. + Cons: Might not be as concise or readable as the Lodash implementation. * **elements.filter((v, i, a) => a.indexOf(v) === i)**: + Pros: Self-contained, no external dependencies required. + Cons: This approach is less efficient due to the use of `indexOf()` and the additional filtering step. When considering which approach to take, think about the trade-off between performance, conciseness, and maintainability. If you prioritize high performance, **_.uniq(elements)** might be the best choice. For a more self-contained solution with potentially lower overhead, consider **[...new Set(elements)]**. The third approach is suitable if readability is your primary concern. Other alternatives to these approaches include: * Using `Array.prototype.reduce()` and an accumulator object to keep track of unique elements. * Utilizing `Array.prototype.map()` and checking for uniqueness in the callback function. * Implementing a custom unique value extraction algorithm using bit manipulation or hashing. For this specific benchmark, it's interesting to see how Lodash's implementation (`_.uniq`) outperforms the more lightweight approaches (`[...new Set(elements)]` and `elements.filter()`). However, the performance difference might not be noticeable in all scenarios.
Related benchmarks:
unique elements in array using filter - lodash 4.17.21
Lodash - uniq2
returns unique values in array
unique elements in array using filter fork
Comments
Confirm delete:
Do you really want to delete benchmark?