<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js'></script>
var myArr = Array.from({
length: 60000
}, () => (Math.floor(Math.random() * 1000)));
var l = new Set(myArr);
return [l];
return _.uniq(myArr);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set | |
Array |
Test name | Executions per second |
---|---|
Set | 1010.2 Ops/sec |
Array | 965.0 Ops/sec |
Let's dive into the world of MeasureThat.net and analyze the provided benchmark.
What is being tested?
The provided benchmark measures the performance difference between two approaches for removing duplicates from an array:
Set
data structure (Benchmark Definition: var l = new Set(myArr); return [...l];
)uniq
function from the Lodash library (Benchmark Definition: return _.uniq(myArr);
)Options being compared
The benchmark compares two approaches, both of which aim to remove duplicates from an array. The main difference between them is:
Set
: This approach uses a mutable data structure (Set
) to store unique values. It iterates over the input array and adds each value to the set. Since sets only allow unique values, this approach automatically removes duplicates. Finally, it returns an array containing all values in the set.uniq
function: This approach uses a library function that implements a stable sort and then filters out duplicates.Pros and Cons
Set
approach:uniq
function:Library: Lodash
Lodash is a popular JavaScript utility library that provides various functions for tasks like string manipulation, array and object manipulation, and more. The uniq
function is one of the many useful utilities provided by Lodash.
Special JS feature or syntax
There are no special JavaScript features or syntax used in this benchmark. Both approaches use standard JavaScript syntax and data structures.
Other alternatives
If you wanted to implement a custom duplicate removal approach without using a library like Lodash, you could consider:
Array.prototype.filter()
combination with the includes()
methodMap
or an object with a single keyHowever, these alternatives would likely be less efficient and more verbose than the approaches used in this benchmark.
I hope this explanation helps you understand what's being tested on MeasureThat.net!