Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs Array.from(new Set()) vs spread new Set()
(version: 0)
Comparing performance of:
Spread vs use lodash vs Array.from
Created:
5 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>
Tests:
Spread
return [...new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])]
use lodash
return _.uniq([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]);
Array.from
return Array.from( new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]) );
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:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
1469786.2 Ops/sec
use lodash
5239625.5 Ops/sec
Array.from
2137647.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is comparing three approaches to remove duplicates from an array: 1. `lodash uniq` 2. `Array.from(new Set())` (also known as using a `Set` object to convert an array to a set and then back to an array) 3. Using the spread operator (`new Set()` followed by the spread operator (`...`)) **Options Being Compared** The three options are being compared in terms of performance, specifically execution speed. * **`lodash uniq`**: This is a library function from Lodash that removes duplicates from an array. * **`Array.from(new Set())`**: This approach converts an array to a `Set` object (which automatically removes duplicates) and then back to an array using the spread operator (`...`). * **Using the spread operator (`new Set()` followed by the spread operator (`...`)**): This approach creates a new set from the original array and then uses the spread operator to create a new array with unique elements. **Pros and Cons** Here's a brief summary of each approach: 1. **`lodash uniq`**: * Pros: Easy to use, efficient, and well-maintained. * Cons: Requires an additional library dependency (Lodash). 2. **`Array.from(new Set())`**: * Pros: Lightweight, efficient, and doesn't require any additional dependencies. * Cons: May be slower than the `lodash uniq` approach due to the conversion overhead. 3. **Using the spread operator (`new Set()` followed by the spread operator (`...`)`**: * Pros: Simple, lightweight, and can be optimized for performance. * Cons: May require additional processing steps to remove duplicates. **Library and Its Purpose** The Lodash library is a popular collection of reusable JavaScript functions. The `uniq` function from Lodash removes duplicate elements from an array while preserving the original order. **Special JS Feature or Syntax (None)** There are no special JavaScript features or syntaxes being used in this benchmark. All code snippets follow standard ECMAScript syntax. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * Using `Set.prototype.filter()` or `Array.prototype.filter()` to remove duplicates. * Utilizing the `reduce()` method with an accumulator and callback function to process the array. * Leveraging modern JavaScript features like `Map` objects or `WeakMaps` for efficient data structures. However, these alternatives may not provide a direct comparison with the original three options in this benchmark.
Related benchmarks:
Lodash uniqBy vs Set vs Set spread
lodash uniq vs spread new Set()
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?