Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs Array.from(new Set()) vs spread new Set() 2
(version: 1)
Comparing performance of:
Spread vs use lodash vs Array.from vs just set
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> const arr = (new Array(10000)).fill().map(e => Math.floor(Math.random() * 100)); </script>
Tests:
Spread
return [...new Set(arr)]
use lodash
return _.uniq(arr);
Array.from
return Array.from( new Set(arr) );
just set
new Set(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Spread
use lodash
Array.from
just set
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 provided JSON benchmark and explain what is tested, compared, and their pros/cons. **Benchmark Definition** The benchmark measures the performance of three approaches to remove duplicates from an array: 1. `Array.from(new Set(arr))`: This approach uses the `Set` object to automatically eliminate duplicate elements. 2. `_uniq(arr)`: This approach uses the Lodash library's `uniq` function, which removes duplicates from an array. 3. `new Set(arr)`: This approach creates a new `Set` object and adds all elements from the original array. **Options Compared** The benchmark compares these three approaches to remove duplicates from an array of 10,000 random integers. **Pros and Cons** Here's a brief summary of each approach: 1. **Array.from(new Set(arr))**: * Pros: Efficient and concise way to eliminate duplicates. * Cons: May not be suitable for large arrays or complex data structures, as it uses the `Set` object internally. 2. **_uniq(arr)_ (Lodash)**: * Pros: Widely used and well-tested library function that can handle various edge cases. * Cons: Adds an extra dependency on Lodash and may have performance overhead due to its complexity. 3. **new Set(arr)**: * Pros: Simple and lightweight way to create a new set of unique elements. * Cons: May not be as efficient as `Array.from(new Set(arr))` for large arrays, as it uses additional memory to store the set. **Library: Lodash** Lodash is a popular JavaScript library that provides a wide range of utility functions. The `_uniq` function in this benchmark removes duplicate elements from an array and returns a new array with unique elements. **Special JS Features/Syntax** None mentioned in the provided JSON. **Other Alternatives** If you want to explore alternative approaches, here are a few options: 1. **Using `filter()`**: You can use the `filter()` method to remove duplicates from an array by comparing each element with its neighbors. ```javascript arr.filter((value, index) => arr.indexOf(value) === index) ``` 2. **Using `reduce()`**: You can use the `reduce()` method to combine unique elements into a new array. ```javascript arr.reduce((uniqueArr, value) => { if (!uniqueArr.includes(value)) { uniqueArr.push(value); } return uniqueArr; }, []) ``` 3. **Using a simple loop**: You can use a simple loop to iterate over the array and add each element to a new array only if it's not already present. ```javascript const uniqueArr = []; for (const value of arr) { if (!uniqueArr.includes(value)) { uniqueArr.push(value); } } ``` These alternatives may have different performance characteristics compared to the approaches tested in this benchmark.
Related benchmarks:
_.uniq() vs Set() over large array
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
lodash uniq vs spread new Set() medium size
lodash uniq vs Array.from(new Set()) vs spread new Set() [big arrays 2]
lodash uniq vs set spread
Comments
Confirm delete:
Do you really want to delete benchmark?