<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var arr = Array.from({length: 40}, () => Math.floor(Math.random() * 40));
var newArr = null;
newArr = _.uniqWith(arr, _.isEqual)
newArr = new Set(arr)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.uniqWith(arr, _.isEqual).length | |
new Set(arr).size |
Test name | Executions per second |
---|---|
_.uniqWith(arr, _.isEqual).length | 1362417.5 Ops/sec |
new Set(arr).size | 1230021.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's tested in the provided JSON.
Benchmark Definition
The benchmark is designed to compare two approaches for finding unique elements in an array:
_.uniqWith(arr, _.isEqual)
from the Lodash library.new Set(arr).size
.Options Compared
These two options are compared because they have different performance characteristics.
_.uniqWith(arr, _.isEqual)
: This approach uses the Lodash library's uniqWith
function, which takes a callback function as an argument. In this case, the callback is set to _.isEqual
, which checks for equality using the ===
operator. This approach is likely to be slower because it involves an additional function call and potentially more memory allocations.new Set(arr).size
: This approach uses the built-in JavaScript method Set
. A Set
object in JavaScript is a collection of unique values, and its size
property returns the number of elements in the set. This approach is likely to be faster because it's a native JavaScript method that doesn't involve any additional function calls or memory allocations.Pros and Cons
Here are some pros and cons of each approach:
_.uniqWith(arr, _.isEqual)
:new Set(arr).size
:Set
, potentially harder to implement.Lodash Library
The Lodash library is a popular utility library for JavaScript that provides a wide range of functions for tasks like array manipulation, string processing, and more. In this case, the uniqWith
function is used to find unique elements in an array while preserving the original order.
Special JS Feature/Syntax (None)
There are no special JavaScript features or syntaxes being tested in this benchmark.
Other Alternatives
If you're looking for alternative approaches to finding unique elements in an array, here are a few options:
Array.prototype.filter()
and Array.prototype.indexOf()
: This approach involves filtering the array to find unique elements using filter()
and then iterating over the resulting array to count the number of unique elements using indexOf()
.Array.prototype.reduce()
and Set
: This approach involves reducing the array to a set of unique values using reduce()
and then counting the size of the resulting set.Array.prototype.forEach()
and Set
: This approach involves iterating over the array using forEach()
and adding each element to a set, which automatically eliminates duplicates.Here's some sample code for these alternative approaches:
// Using Array.prototype.filter() and Array.prototype.indexOf()
const uniqueElements = arr.filter((element) => {
return arr.indexOf(element) === -1;
}).length;
// Using Array.prototype.reduce() and Set
const uniqueElements = arr.reduce((set, element) => {
set.add(element);
return set.size;
}, new Set()).size;
// Using Array.prototype.forEach() and Set
arr.forEach((element) => {
const set = new Set();
set.add(element);
// ...
});
Note that these alternative approaches may have different performance characteristics than the original benchmark.