<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
var l = new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]);
return l;
var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7];
return _.uniq(l);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set | |
Array |
Test name | Executions per second |
---|---|
Set | 3250077.2 Ops/sec |
Array | 5460852.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
The benchmark is comparing the performance of two approaches: using a Set
data structure and using the uniq()
function from the Lodash library to remove duplicates from an array.
Options being compared:
Set
data structure to store unique elements.uniq()
function from Lodash to remove duplicates from an array.Pros and Cons of each approach:
Library used:
The uniq()
function is part of the Lodash library. Lodash is a popular JavaScript utility library that provides a collection of functional programming helpers, including data manipulation functions like uniq()
. The library is widely adopted and well-maintained, making it a great choice for many developers.
Special JS feature or syntax:
There are no special features or syntax used in this benchmark. However, using the Set
data structure does rely on JavaScript's built-in Set
object, which was introduced in ECMAScript 2015 (ES6).
Benchmark preparation code and HTML code:
The benchmark preparation code is simple:
var l = new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]);
return l;
The HTML code includes a script tag to load the Lodash library:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Other alternatives:
If you wanted to implement this benchmark without using a Set
data structure or the Lodash library, you could use other approaches like:
array-duplicates
or uniqby
.However, these alternatives would likely have different performance characteristics and might not be as efficient as using a Set
data structure or the Lodash library.