Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS fastest unique array Set vs uniq vs filter
(version: 0)
Compare performance between Set, lodash uniq, filter
Comparing performance of:
Set vs uniq vs filter
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Script Preparation code:
var arr = [ 18, 2, 1, 13, 12, 7, 6, 0, 8, 15, 7, 9, 9, 18, 3, 18, 3, 9, 3, 8, 7, 12, 7, 14, 4, 19, 10, 5, 2, 19, 1, 14, 19, 18, 7, 12, 10, 2, 4, 11, 9, 4, 12, 2, 18, 6, 7, 10, 9, 3, 11, 9, 2, 12, 15, 0, 14, 3, 5, 18, 16, 16, 9, 15, 5, 9, 16, 6, 13, 4, 19, 5, 11, 18, 18, 6, 19, 5, 4, 2, 3, 4, 11, 9, 16, 17, 10, 15, 14, 7, 11, 0, 5, 13, 7, 14, 13, 16, 9, 19, 4, 0, 4, 0, 5, 2, 15, 15, 13, 10, 3, 8, 18, 7, 10, 4, 4, 16, 14, 1, 4, 19, 1, 14, 15, 16, 5, 4, 0, 0, 12, 0, 4, 7, 0, 4, 1, 9, 14, 12, 12, 1, 2, 6, 0, 10, 6, 19, 12, 4, 7, 11, 13, 2, 1, 19, 9, 6, 15, 9, 2, 11, 14, 5, 18, 13, 16, 19, 8, 12, 12, 4, 5, 1, 10, 19, 0, 2, 6, 19, 4, 19, 7, 18, 18, 5, 16, 18, 13, 8, 9, 4, 14, 12, 10, 10, 19, 11, 8, 9, 13, 6, 0, 10, 6, 6, 5, 8, 3, 18, 11, 12, 3, 14, 12, 8, 7, 12, 6, 4, 4, 2, 19, 17, 14, 11, 2, 16, 13, 10, 19, 9, 18, 12, 9, 16, 6, 14, 13, 15, 2, 15, 19, 9, 19, 0, 5, 11, 0, 15, 17, 0, 9, 18, 0, 15, 11, 4, 3, 9, 2, 18, 18, 6, 19, 11, 3, 0, 7, 5, 9, 3, 11, 18, 0, 10, 1, 8, 1, 18, 7, 3, 0, 13, 11, 7, 10, 5, 19, 11, 4, 4, 0, 18, 9, 3, 3, 19, 9, 4, 16, 13, 13, 14, 18, 12, 17, 2, 12, 1, 13, 12, 11, 8, 18, 14, 5, 18, 0, 3, 5, 17, 3, 16, 3, 11, 16, 19, 13, 11, 2, 15, 6, 12, 0, 13, 2, 2, 16, 17, 10, 18, 3, 18, 5, 18, 3, 8, 0, 4, 13, 9, 18, 7, 15, 12, 3, 11, 3, 8, 12, 6, 10, 3, 2, 18, 3, 19, 10, 19, 13, 19, 15, 2, 14, 18, 15, 10, 18, 19, 7, 10, 3, 16, 7, 7, 8, 19, 1, 15, 9, 18, 18, 5, 14, 11, 11, 14, 5, 2]
Tests:
Set
var u = new Set(arr); return u;
uniq
var u = _.uniq(arr) return u;
filter
var u = arr.filter((v, i, a) => a.indexOf(v) === i) return u;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
uniq
filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
277056.2 Ops/sec
uniq
263517.5 Ops/sec
filter
359333.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, comparing the performance of different approaches. The provided benchmark definition JSON and individual test cases describe three scenarios: using a Set data structure, Lodash's `uniq` function, and Array filtering. **Benchmark Definition** The benchmark definition JSON describes a scenario where an array is created with 50 elements, and then: 1. A new Set object is created from the array (`var u = new Set(arr); return u;`). 2. The Lodash library is used to remove duplicates from the array using `_.uniq(arr)`. 3. The Array filtering method is used to filter out elements that appear more than once in the array (`var u = arr.filter((v, i, a) => a.indexOf(v) === i); return u;`). **Individual Test Cases** Each individual test case describes a specific benchmark definition: 1. **Set**: Creates a new Set object from the input array and returns it. 2. **uniq**: Uses Lodash's `uniq` function to remove duplicates from the input array and returns the result. 3. **filter**: Filters out elements that appear more than once in the input array using Array filtering and returns the result. **Latest Benchmark Result** The latest benchmark result shows the performance metrics for each test case: 1. **Set**: 112609.8828125 executions per second (fps) on a Chrome 120 browser on a Desktop platform with Mac OS X 10.15.7. 2. **uniq**: 113232.2734375 fps on the same conditions. 3. **filter**: 130057.484375 fps on the same conditions. **Comparison** Based on the latest benchmark result, the Array filtering method outperforms both the Set data structure and Lodash's `uniq` function by a significant margin (over 10 times). This suggests that the filtering approach is more efficient in this specific scenario. However, it's essential to note that these results may vary depending on the specific use case, array size, and browser environment. MeasureThat.net provides a useful platform for comparing different approaches and identifying optimal solutions for specific problems.
Related benchmarks:
unique elements in array using filter v2
unique elements in array using filter v2.3
unique elements in array using filter - large array
lodash uniq vs Array.from(new Set()) vs spread new Set() vs array.filter_bigger
Comments
Confirm delete:
Do you really want to delete benchmark?