Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique elements in array using filter fork
(version: 0)
Comparing performance of:
_.uniq vs set vs uniq by filter vs uniq by qiankun
Created:
3 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 = [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)
uniq by qiankun
elements.filter(function filter(element) { return element in this ? false : ((this)[element] = true); }, Object.create(null));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
_.uniq
set
uniq by filter
uniq by qiankun
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 provided benchmark definition and test cases. **What is being tested?** The provided benchmark tests four different approaches to find unique elements in an array: 1. `_.uniq` (Lodash): This function uses a Set data structure to keep track of unique elements. 2. `[...new Set(elements)]`: This approach converts the array to a Set, which automatically removes duplicates. 3. `elements.filter((v, i, a) => a.indexOf(v) === i)`: This approach uses the `indexOf` method to check if an element is present in the original array at the same index it appears in the new array. 4. `elements.filter(function filter(element) { ... })`: This approach uses a closure (an anonymous function) to keep track of unique elements. **Options compared** The four approaches are compared in terms of their execution speed, with the goal of finding the fastest way to find unique elements in an array. **Pros and Cons of each approach:** 1. `_.uniq` (Lodash): * Pros: + Fast and efficient + Does not modify the original array * Cons: + Requires Lodash library to be included 2. `[...new Set(elements)]`: * Pros: + Simple and concise + No additional libraries needed * Cons: + May have higher overhead due to the conversion process 3. `elements.filter((v, i, a) => a.indexOf(v) === i)`: * Pros: + Does not require an additional library * Cons: + Has higher constant time complexity (O(n)) compared to other approaches 4. `elements.filter(function filter(element) { ... })`: * Pros: + No additional libraries needed * Cons: + More complex and harder to read due to the closure **Library description** Lodash is a JavaScript library that provides a lot of useful functions for common tasks, such as array manipulation (`_.uniq`), string manipulation, and more. **Special JS feature or syntax** None of the test cases use special JavaScript features or syntax. They are all basic array operations. **Other alternatives** Some other approaches to find unique elements in an array include: * Using `Array.prototype.reduce()` * Using a custom implementation with a hash table data structure * Using a library like `lodash-es` (a more modern and minimalist version of Lodash) These alternative approaches may have their own pros and cons, but are not tested in the provided benchmark. I hope this explanation helps! Let me know if you have any further questions.
Related benchmarks:
unique elements in array using filter
unique elements in array using filter - lodash 4.17.21
Lodash - uniq2
unique elements in array using filter fork2
Comments
Confirm delete:
Do you really want to delete benchmark?