Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Uniq Array - lodash uniq vs Array from 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
const res = Array.from(new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])); return res;
Array
var res = _.uniq([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]); return res;
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided JSON benchmark. **Benchmark Definition** The benchmark measures the performance of two approaches to remove duplicates from an array: 1. **Array.from(new Set([array]))**: This approach uses the `Set` data structure to remove duplicates from the array. The `Set` object in JavaScript is a collection of unique values, and when you convert an array to a set using `Set()`, it automatically removes any duplicate elements. 2. **_uniq(array)**: This approach uses the Lodash library's `_uniq()` function, which removes duplicate elements from an array. **Options Compared** The two options compared in this benchmark are: * `Array.from(new Set([array]))` * `_uniq(array)` These approaches have different performance characteristics, pros, and cons: * **`Array.from(new Set([array]))`**: + Pros: This approach is generally faster because it uses the optimized implementation of the `Set` data structure. It also has better cache locality, as the resulting array is contiguous in memory. + Cons: This approach requires a new set object to be created and iterated over, which can lead to higher memory usage and slower performance on very large arrays. * `_uniq(array)`: + Pros: Lodash's `_uniq()` function is implemented in C++ and optimized for performance. It also avoids the overhead of creating a new set object. + Cons: This approach relies on Lodash, which may not be included in all browsers or environments. Additionally, its performance can vary depending on the specific implementation. **Library (Lodash)** The `Set` data structure is a built-in JavaScript feature, but it's only available in ECMAScript 2015 and later standards. In earlier versions of JavaScript, you would need to use libraries like Lodash or underscore.js to implement a similar functionality. **JavaScript Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. It's purely functional programming with JavaScript. **Other Alternatives** If you're interested in exploring other approaches, here are some alternatives: * **`Array.prototype.filter()`**: You can use the `filter()` method to remove duplicates from an array, like this: `[...new Set(array)].filter((v, i) => array.indexOf(v) === i)` * **`Array.prototype.reduce()`**: Another approach is to use the `reduce()` method to accumulate unique elements into a new array: `array.reduce((acc, v) => acc.includes(v) ? acc : [...acc, v], [])` * **Native `Set` implementation**: If you're using ECMAScript 2015 or later, you can use the native `Set` data structure directly. Keep in mind that these alternatives may have different performance characteristics and overhead compared to the two options being benchmarked.
Related benchmarks:
lodash uniq vs native uniq
Lodash uniq vs Set to unique array
lodash uniq vs set - 3
Lodash - uniq
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?