Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
bigArray uniq vs set vs reduce few duplicate
(version: 0)
Comparing performance of:
Array from Set vs Lodash Uniq vs Array reduce
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var testArr = Array.from({length: 10000}, () => Math.floor(Math.random() * 50))
Tests:
Array from Set
[...new Set(testArr)]
Lodash Uniq
_.uniq(testArr)
Array reduce
testArr.reduce((ids, cid) => {return ids.some((pid) => pid === cid) ? ids : ids.concat(cid);}, [])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array from Set
Lodash Uniq
Array reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array from Set
6952.8 Ops/sec
Lodash Uniq
6338.8 Ops/sec
Array reduce
4487.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining what's tested in this benchmark. **Benchmark Overview** The benchmark is designed to compare the performance of three approaches for removing duplicates from an array: 1. Using a Set data structure 2. Using the Lodash `uniq` function 3. Using the Array reduction technique with `some` and `concat` Each test case uses the same input array `testArr`, which is generated randomly. **Set Data Structure Approach** This approach uses the built-in `Set` object in JavaScript, which automatically removes duplicates by using a hash table to store unique values. The benchmark tests how fast this approach can remove duplicates from the `testArr` array. Pros: * Simple and efficient * Built-in data structure Cons: * May have higher memory usage due to the Set's internal storage * Not directly comparable to other approaches without additional transformations **Lodash `uniq` Function** This approach uses the Lodash library, which provides a convenient way to remove duplicates from an array. The `uniq` function takes an input array and returns a new array with unique values. Pros: * Convenient and easy to use * Provides a standardized solution for removing duplicates Cons: * May introduce additional memory usage due to the creation of a new array * Dependence on external library may be seen as a drawback **Array Reduction Technique** This approach uses the Array reduction technique, which iterates over the input array and builds an output array by accumulating unique values. The benchmark tests how fast this approach can remove duplicates from the `testArr` array. Pros: * Can be highly optimized for performance * Does not require additional memory allocation Cons: * May have a higher computational overhead compared to other approaches * Can be more complex and harder to understand **Library Overview** The Lodash library is used in this benchmark, which provides a set of utility functions for common tasks such as removing duplicates. The `uniq` function is specifically designed to remove duplicates from an array. **Special JavaScript Feature/Syntax** None of the test cases use any special JavaScript features or syntax that would require additional explanation. **Alternative Approaches** Other approaches for removing duplicates from an array include: * Using a custom implementation with manual iteration and storage * Using a data structure like a Trie or a Bloom filter, which can be more memory-efficient but may have higher computational overhead. * Using a parallelized approach to take advantage of multi-core processors. Keep in mind that the performance differences between these approaches will depend on the specific use case, input data, and target hardware.
Related benchmarks:
Unique lodash vs vanilla
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
bigArray uniq vs set vs reduce
lodash uniq vs Array.from(new Set()) vs spread new Set() [big arrays 2]
Comments
Confirm delete:
Do you really want to delete benchmark?