<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
let counter = 1e5;
const arr = [];
while(counter--) {
arr.push(i);
arr.push(i);
}
_.uniq(arr);
let counter = 1e5;
const set = new Set();
while(counter--) {
set.add(i);
set.add(i);
}
[set];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash Uniq | |
Javascript Set |
Test name | Executions per second |
---|---|
Lodash Uniq | 377.0 Ops/sec |
Javascript Set | 1228.2 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared options, their pros and cons, and other considerations.
Benchmark Overview
The benchmark compares two approaches to remove duplicates from an array:
uniq
function from the Lodash library, which creates a new array with unique values.Set
object to store unique values and then converts it back to an array.Options Compared
The two options are compared in terms of:
Pros and Cons
Other Considerations
Library: Lodash
Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks like array manipulation, string manipulation, and functional programming. The uniq
function is one of its most useful utilities, making it easy to remove duplicates from arrays.
The provided benchmark code snippet includes the necessary import statement for Lodash, allowing users to easily compare the performance of this approach with JavaScript Set.
Special JS Feature/ Syntax
This benchmark does not require any special JavaScript features or syntax, making it accessible to a wide range of developers.
Alternatives
Other alternatives to remove duplicates from an array include:
Array.prototype.filter()
and Array.prototype.includes()
.These alternatives may have varying trade-offs in terms of performance, readability, and maintainability, but can be worth exploring depending on specific use cases or project requirements.