Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs Array.from(new Set()) vs spread new Set() [big arrays 2]
(version: 0)
Comparing performance of:
Spread vs use lodash vs Array.from
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 arr = new Array(10_000).fill(1).map(() => Math.round(Math.random() * 20))
Tests:
Spread
return [...new Set(arr)]
use lodash
return _.uniq(arr);
Array.from
return Array.from( new Set(arr) );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Spread
use lodash
Array.from
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
15 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
3298.2 Ops/sec
use lodash
3293.6 Ops/sec
Array.from
3237.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The benchmark is designed to compare three approaches for removing duplicate values from an array: 1. Using `Array.from(new Set(arr))`: This method creates a new set from the input array and then converts it back to an array using `Array.from()`. 2. Using the `uniq()` function from the Lodash library: This function takes an array as input and returns a new array with duplicate values removed. 3. Using the spread operator (`...`) with `new Set(arr)`: This method creates a new set from the input array and then uses the spread operator to convert it back to an array. **Options Compared** The three approaches are compared in terms of performance, which is measured by the number of executions per second (ExecutionsPerSecond). **Pros and Cons** 1. **Array.from(new Set(arr))**: * Pros: Simple and efficient way to remove duplicates. * Cons: Creates a new set, which can be slower than other methods for large arrays due to the overhead of hash-based data structures. 2. **Lodash uniq()**: * Pros: Fast and efficient, especially for larger arrays. * Cons: Requires including an additional library (Lodash) in the benchmark setup, which may add overhead. 3. **Spread new Set()**: * Pros: Simple and readable syntax. * Cons: May be slower than other methods due to the overhead of creating a new set and then spreading it. **Other Considerations** The benchmark is likely testing for performance, but it's also worth considering the readability and maintainability of each approach. For example, using `Array.from(new Set(arr))` may require more code and setup compared to using `_.uniq()` from Lodash. **Library: Lodash** Lodash (also known as underscore) is a popular JavaScript library that provides a collection of utility functions for tasks such as data manipulation, array operations, and string manipulation. The `uniq()` function is one of the many useful utilities provided by Lodash. **Special JS Feature/Syntax** There doesn't appear to be any special JavaScript features or syntax used in this benchmark beyond what's necessary for the three approaches being compared. If there were any special features or syntax, I'd be happy to explain them! **Alternatives** Other alternatives for removing duplicates from an array might include: 1. Using a for loop with an object to keep track of unique values. 2. Using `filter()` and checking for duplicate values. 3. Using a custom function written in a specific programming language (e.g., C++ or Rust) that can be compiled to JavaScript. However, these alternatives may not be as efficient or concise as the three approaches being compared in this benchmark.
Related benchmarks:
Unique lodash vs vanilla
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 set spread
Comments
Confirm delete:
Do you really want to delete benchmark?