Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs Array.from(new Set()) vs spread new Set() v3 strings
(version: 0)
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 => "asd" + String.fromCharCode(Math.floor(97 + Math.random() * 25))); </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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
4087.3 Ops/sec
use lodash
4340.9 Ops/sec
Array.from
4339.0 Ops/sec
just set
4059.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches for removing duplicates from an array: 1. Using the `Array.from()` method with a `Set` object (`Array.from(new Set(arr))`) 2. Using the `lodash uniq()` function (`_.uniq(arr)`) 3. Simply creating a new `Set` object without using any additional methods (`new Set(arr)`) **Options Compared** The three options are compared in terms of their execution time, measured in executions per second. **Pros and Cons of Each Approach:** 1. **Array.from(new Set())** * Pros: + Easy to understand and implement + No external dependencies (no `lodash` library) * Cons: + Can be slower due to the overhead of creating a new array from a set + Requires at least JavaScript 6.0 for `Set` support 2. **_.uniq(arr)** * Pros: + Fast and efficient, thanks to optimized implementation in `lodash` + Easy to use with the `_` function and no need to create a new array or set 3. **new Set(arr)** * Pros: + Very lightweight (no overhead of creating an array) + Fast lookup times for duplicate removal * Cons: + Requires at least JavaScript 6.0 for `Set` support + Can be slower due to the overhead of iterating over the original array **Library: Lodash** The benchmark uses the `lodash` library, which provides a convenient and efficient way to remove duplicates from an array using the `_uniq()` function. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntax used in this benchmark. **Alternative Approaches** If you're looking for alternative approaches to removing duplicates from an array, here are a few examples: 1. Using `reduce()`: You can use the `reduce()` method to iterate over the array and keep track of unique elements. 2. Using `filter()`: You can use the `filter()` method to remove duplicate elements from the array by creating a new array with only the first occurrence of each element. 3. Using a custom implementation: You can implement your own algorithm for removing duplicates, such as using an object to keep track of unique elements. Keep in mind that these alternative approaches may have varying levels of performance and readability compared to the three options being benchmarked here.
Related benchmarks:
Lodash uniqBy vs Set vs Set spread
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?