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 vs loop
(version: 0)
Comparing performance of:
Spread vs use lodash vs Array.from vs just set vs Loop
Created:
one year 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> 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)
Loop
const res = new Set() for (let i of arr) { res.add(i) }
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
just set
Loop
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
7512.0 Ops/sec
use lodash
7528.3 Ops/sec
Array.from
7555.0 Ops/sec
just set
7637.5 Ops/sec
Loop
8163.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is testing four different approaches to remove duplicates from an array of strings: 1. Using `Array.from(new Set())` 2. Using Lodash's `uniq` function 3. Creating a new set using just the `Set` constructor 4. Using a loop to add elements to a new set **Options Compared** The benchmark is comparing the performance of these four approaches on an array of 10,000 strings, where each string is generated randomly. **Pros and Cons of Each Approach:** 1. **Array.from(new Set())** * Pros: Efficient, concise, and easy to read. * Cons: May not be supported in older browsers or environments that don't support `Set`. 2. **Lodash's uniq function** * Pros: Well-tested, widely used, and provides additional functionality (e.g., sorting). * Cons: Adds an external dependency, may have a performance overhead due to the function call. 3. **just Set constructor** * Pros: Lightweight, no dependencies, easy to understand. * Cons: May be slower than other approaches due to the need for manual iteration. 4. **Loop** * Pros: Control over the iteration process, potentially optimized for specific use cases. * Cons: More verbose, error-prone, and may not be as efficient as other approaches. **Library Used:** The benchmark uses Lodash's `uniq` function, which is a popular utility library for JavaScript. The `uniq` function removes duplicates from an array while preserving the original order of elements. **Special JS Feature or Syntax:** None mentioned in this specific benchmark. **Other Considerations:** * Browser support: The benchmark is run on Chrome 125, which may not be compatible with older browsers. * Environment: The benchmark is run on a Linux desktop environment, which may affect performance due to differences in system resources and caching behavior. * Test setup: The benchmark creates an array of random strings, which may not represent real-world scenarios. **Alternatives:** If you're looking for alternative approaches to remove duplicates from an array, consider: 1. Using `map` and `filter`: ```javascript const res = arr.map((x) => x).filter((x, i, a) => a.indexOf(x) === i); ``` 2. Using a library like `lodash-es` (ES version of Lodash), which provides a similar `uniqBy` function for more complex scenarios. 3. Implementing a custom duplicate removal algorithm using bitwise operations or other optimization techniques. Keep in mind that the best approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Split Method vs Lodash (v4.17.21)
lodash uniq vs Array.from(new Set()) vs spread new Set() v3 strings
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?