<script src="https://cdn.jsdelivr.net/npm/lodash/lodash.min.js"></script>
var firstEqual = [];
var secondEqual = [];
for (var i=0; i<=10000; 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 | 651.4 Ops/sec |
Javascript Set | 718.8 Ops/sec |
Let's break down the benchmark and its components.
Benchmark Definition
The benchmark is designed to compare two approaches for removing duplicates from an array: Lodash's uniq
function and JavaScript's built-in Set
object.
Options Compared
Two options are being compared:
uniq
function from the Lodash library, which removes duplicate elements from an array.Set
object in JavaScript, which automatically removes duplicates when used with arrays.Pros and Cons
Library and Purpose
The lodash
library is a popular JavaScript utility belt that provides various functions for tasks like array manipulation, string manipulation, and more. In this case, the uniq
function is used to remove duplicates from an array.
Special JS Feature or Syntax
None are mentioned in the provided code. However, it's worth noting that some JavaScript versions have had experimental features or syntaxes related to sets, but these were not widely adopted until recent versions of the language.
Other Alternatives
Besides Lodash and JavaScript Set, other alternatives for removing duplicates from an array include:
Array.prototype.filter()
and keeping track of unique elements in an external data structure.underscore
(similar to Lodash) or ramda
.Benchmark Preparation Code
The script preparation code generates two arrays with 10,000 elements each. These arrays are then used as input for both benchmark tests.
Individual Test Cases
Each test case runs the specified function on the generated array:
uniq
function from Lodash is called on the combined array (arrayToDedup
).Set
object is created from the combined array, and its size (i.e., the number of unique elements) is obtained.Latest Benchmark Result
The latest benchmark result shows that the JavaScript Set approach outperforms Lodash Uniq in terms of executions per second on a Chrome 89 browser running on Windows.