Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
(version: 0)
Comparing performance of:
Spread vs use lodash vs Array.from vs For vs For memory optimized
Created:
2 years ago
by:
Guest
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 array = []; for (let i = 0; i < 100000; i++) {array.push(~~(Math.random() * 2000))}
Tests:
Spread
return [...new Set(array)]
use lodash
return _.uniq(array);
Array.from
return Array.from( new Set(array) );
For
const visited = {}; const uniques = []; const initial = array; for(let i=0;i<initial.lenght;i++){ if(!visited[initial[i]]) { visited[initial[i]] = true; uniques.push(initial[i]); } } return uniques
For memory optimized
const visited = {}; const uniques = []; const initial = array; for(let i=0;i<initial.lenght;i++){ if(!(initial[i] in visited)) { visited[initial[i]] = undefined; uniques.push(initial[i]); } } return uniques
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Spread
use lodash
Array.from
For
For memory optimized
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
535.4 Ops/sec
use lodash
538.8 Ops/sec
Array.from
559.1 Ops/sec
For
15491293.0 Ops/sec
For memory optimized
15547500.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares the performance of four different approaches to find unique elements in an array: 1. `Array.from(new Set(array))` 2. `_.uniq(array)` (using Lodash library) 3. `for` loop with manual handling of unique elements 4. `for` loop with optimized memory usage **Options Comparison** The benchmark tests each approach on a fixed dataset of 100,000 random integers between 0 and 2000. * **Array.from(new Set(array))**: Uses the `Set` data structure to remove duplicates from the array. + Pros: Simple and efficient, no need to manually handle unique elements. + Cons: Requires modern JavaScript features (ES6+), may not work in older browsers or environments. * **_.uniq(array)` (using Lodash library)**: Uses the `uniq` function from Lodash to remove duplicates from the array. + Pros: Convenient and efficient, no need to manually handle unique elements. + Cons: Requires external dependency (Lodash), may not work in older browsers or environments. * **for loop with manual handling**: Manually iterates through the array to find unique elements using a `visited` object. + Pros: Platform-independent, no external dependencies required. + Cons: More complex and error-prone, requires careful handling of edge cases. * **for loop with optimized memory usage**: Optimized version of the manual loop approach, using `in` operator instead of indexing to check for unique elements. + Pros: Efficient and platform-independent, reduces memory usage compared to the original manual loop. + Cons: Still more complex than the Array.from approach. **Library Overview** The Lodash library is a popular utility library that provides various functions for common tasks, including data manipulation. In this benchmark, `_.uniq(array)` is used to remove duplicates from an array. **Special JS Features or Syntax** None mentioned in the provided information. **Benchmark Results** The latest benchmark results show that: 1. **For memory optimized**: The fastest approach, with approximately 15.5 million executions per second. 2. **For**: The next fastest approach, with around 15.4 million executions per second. 3. **Array.from**: The slowest approach, with about 0.56 million executions per second (likely due to modern JavaScript features and dependencies). 4. **Use Lodash**: The slowest approach among the four, with approximately 0.54 million executions per second. **Other Alternatives** Some alternative approaches that could be tested in a benchmark include: * Using `Map` data structure instead of `Set` * Using a more optimized algorithm for finding unique elements * Comparing performance using different array sizes or distributions * Testing performance on different types of input data (e.g., strings, objects)
Related benchmarks:
_.uniq() vs Set() over large array
lodash uniq vs Array.from(new Set()) vs spread new Set() [big arrays 2]
lodash uniq vs set spread
Array.prototype.every vs Lodash every()
Comments
Confirm delete:
Do you really want to delete benchmark?