Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs set spread
(version: 0)
is it faster to use spread notation and a new Set or is it faster to use lodash uniq?
Comparing performance of:
Set vs Array
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 = [...Array(50).keys()].flatMap(a => [...Array(20).keys()])
Tests:
Set
var l = [...new Set(arr)]; return l;
Array
var l = arr; 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:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0 r3dfox/139.0.4
Browser/OS:
Firefox 139 on Windows 8.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
59895.9 Ops/sec
Array
61548.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case for measuring the performance of two approaches: using `Array.prototype.sort()` with a `Set` object (approach A) and using Lodash's `uniq()` function (approach B). The benchmark aims to determine which approach is faster. **Options Compared** The benchmark compares two options: 1. **Approach A: Using a new Set** * Creates a new Set from an array of keys, sorted in ascending order. 2. **Approach B: Using Lodash's `uniq()` function** * Uses the `uniq()` function from Lodash to remove duplicate elements from the original array. **Pros and Cons** ### Approach A (Set) Pros: * Creates a new Set, which is a lightweight data structure optimized for fast lookup and insertion. * Can be faster than Lodash's `uniq()` since it avoids the overhead of a JavaScript function call. Cons: * Requires manual sorting of the keys before creating the Set. * May not preserve the original order of elements if keys are not unique. ### Approach B (Lodash's `uniq()`) Pros: * Preserves the original order of elements. * Avoids manual sorting and creates a new array with unique elements in one operation. Cons: * Requires an external library (Lodash) to be included. * May have additional overhead due to the JavaScript function call. **Other Considerations** * The benchmark uses `Array.prototype.sort()` to sort the keys, which may not be the most efficient sorting algorithm for large datasets. Other sorting algorithms like quicksort or mergesort might be more suitable. * If the input array is very large, the performance difference between these two approaches might be negligible. **Library Used (Lodash)** The `uniq()` function from Lodash is used to remove duplicate elements from the original array. This library provides a convenient and efficient way to perform common array operations like filtering, mapping, and sorting. If you're interested in implementing similar functionality without using an external library, you can create your own implementation of `uniq()` or use other JavaScript libraries like jQuery. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. The test case only uses standard JavaScript syntax for creating arrays and sets, as well as the Lodash library for its `uniq()` function. **Alternatives** If you're interested in exploring alternative approaches to this benchmark, here are some options: 1. **Use a different sorting algorithm**: Implement quicksort or mergesort instead of using `Array.prototype.sort()`. 2. **Use another array manipulation library**: Explore libraries like jQuery's array methods or libraries like Ramda. 3. **Optimize the Set approach**: Use a data structure like an `IntMap` (a map where keys are integers) to improve performance for large datasets. Keep in mind that these alternatives might require more code and may have different trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
lodash uniq vs Array.from(new Set()) vs spread new Set() vs array.filter
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
lodash uniq vs Array.from(new Set()) vs spread new Set() [big arrays 2]
Comments
Confirm delete:
Do you really want to delete benchmark?