<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
var firstEqual = [];
var secondEqual = [];
for (var i=0; i<=1000000; i++) {
firstEqual.push(i);
secondEqual.push(i);
}
var arrayToDedup = [firstEqual, secondEqual];
_.uniq(arrayToDedup);
[new Set(arrayToDedup)]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash Uniq | |
Javascript Set |
Test name | Executions per second |
---|---|
Lodash Uniq | 2.0 Ops/sec |
Javascript Set | 1.9 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Definition
The benchmark is testing two different approaches to remove duplicates from an array:
uniq
that returns a new array with unique elements.Set
data structure to remove duplicates.Options being compared
The benchmark is comparing the performance of these two approaches:
Pros and Cons
Here are some pros and cons of each approach:
Library - Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as data manipulation, string manipulation, and more. The uniq
function in particular is designed to remove duplicates from an array while preserving order.
Special JS feature - Spread operator (...
)
The benchmark uses the spread operator (...
) to convert arrays to new arrays. This syntax was introduced in ECMAScript 2015 (ES6) and allows for more concise way of creating arrays by spreading elements into a new array.
Other alternatives
If you want to implement this benchmark yourself, here are some other approaches you could consider:
Array.prototype.filter()
and checking for equality using ===
Array.prototype.reduce()
with a custom reduce functionSet
data structure in JavaScript (although this would likely be slower than the built-in Set
)Keep in mind that these alternatives may not provide the same level of performance or consistency as the benchmarked approaches.