Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.uniq vs Array.from(new Set())
(version: 0)
Comparing performance of:
Set vs Array
Created:
3 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:
Set
var l = new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]); return Array.from(l);
Array
var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return _.uniq(l);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
6421713.0 Ops/sec
Array
16618923.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Overview** The benchmark compares two approaches to achieve uniqueness in an array: using the `Array.from(new Set())` method and the `_uniq()` function from the Lodash library. The goal is to measure which approach is faster. **Options Compared** 1. **`Array.from(new Set())`**: This method converts the input array into a set, which automatically removes duplicates, and then converts it back into an array. The `Set` object only stores unique values. 2. **Lodash `_uniq()` function**: This function takes an array as input and returns a new array with duplicate elements removed. **Pros and Cons of Each Approach** 1. **`Array.from(new Set())`**: * Pros: + Efficient, as sets are optimized for fast lookups and deletions. + No additional library dependencies required. * Cons: + May not be suitable for very large arrays, as creating a set can consume a lot of memory. 2. **Lodash `_uniq()` function**: * Pros: + More readable code, as it's a familiar library with a simple syntax. + Can handle larger arrays without performance issues (due to Lodash's optimized implementation). * Cons: + Requires an additional library dependency. + May have slower performance due to the overhead of using a separate library. **Library and Its Purpose** The `lodash` library is a popular JavaScript utility library that provides various functions for tasks like array manipulation, object transformation, and more. The `_uniq()` function specifically removes duplicate elements from an array. **Test Case 1: `Set`** This test case creates a new set with duplicates (7) and then converts it to an array using `Array.from()`. The goal is to measure the performance of this approach. **Test Case 2: `Array`** This test case uses Lodash's `_uniq()` function to remove duplicates from an array. The goal is to measure the performance of this approach. **Other Alternatives** 1. **Using `...new Set()`**: This syntax can be used with modern JavaScript (ES6+) to create a new set and then spread its values into an array. 2. **Using `Object.values()` and `filter()`**: Another way to remove duplicates from an array is to convert it to an object, get its values, and then filter out duplicates. In summary, the benchmark compares two approaches for achieving uniqueness in an array: using the `Array.from(new Set())` method and Lodash's `_uniq()` function. The choice of approach depends on trade-offs between performance, memory usage, readability, and library dependencies.
Related benchmarks:
Lodash uniq vs Set to unique array
lodash uniq vs set - 3
Lodash - uniq
lodash uniq vs deconstructed set
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?