Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
bigArray uniq vs set vs reduce
(version: 3)
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() * 10))
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:
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 break down the benchmark test case and explain what is being tested, compared, and their pros and cons. **Benchmark Definition JSON** The provided JSON defines a benchmark with three test cases: 1. `Array from Set`: Tests the execution time of creating an array by converting an object set. 2. `Lodash Uniq`: Tests the execution time of using the Lodash library's `uniq` function to remove duplicates from an array. 3. `Array reduce`: Tests the execution time of using the Array.prototype.reduce() method to find duplicates in an array. **Comparison Options** The three test cases compare different approaches to finding duplicates in an array: 1. **Creating an array by converting an object set**: This approach uses the spread operator (`[...new Set(testArr)]`) to create a new array containing unique elements from the original array. 2. **Using Lodash library's `uniq` function**: This approach uses the `uniq` function from the Lodash library to remove duplicates from the array. 3. **Using Array.prototype.reduce() method**: This approach uses the reduce() method to find duplicates in the array by comparing each element with the accumulated IDs. **Pros and Cons of Each Approach** 1. **Creating an array by converting an object set**: * Pros: Simple, efficient, and effective for small arrays. * Cons: May be slower for very large arrays due to the overhead of creating a new array. 2. **Using Lodash library's `uniq` function**: * Pros: Reusable, well-tested, and optimized for performance. * Cons: Requires an additional dependency (Lodash library), which may not be desired by all users. 3. **Using Array.prototype.reduce() method**: * Pros: Native implementation, easy to understand, and flexible. * Cons: May be slower than the other two approaches due to the overhead of function calls and accumulator updates. **Library Used** The Lodash library is used in one of the test cases (`Lodash Uniq`). The `uniq` function from Lodash is a popular utility function for removing duplicates from arrays, but it requires an additional dependency on the library. **Special JS Feature or Syntax** None of the test cases use any special JavaScript features or syntax. They all rely on standard JavaScript language features and built-in methods (e.g., Array.from(), Set, reduce()). **Other Alternatives** Other alternatives for finding duplicates in an array include: * Using a `Set` object directly: `new Set(testArr)`. This approach is similar to the "Creating an array by converting an object set" method but does not require creating a new array. * Using a custom implementation with loops and conditional statements. This approach can be optimized for specific use cases, but it may be less efficient than the other options. Overall, the benchmark test case provides a useful comparison of different approaches to finding duplicates in an array, highlighting the trade-offs between simplicity, performance, and reusability.
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 few duplicate
lodash uniq vs Array.from(new Set()) vs spread new Set() [big arrays 2]
Comments
Confirm delete:
Do you really want to delete benchmark?