<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
var l = Array.from(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 |
---|---|
new Set | |
uniq |
Test name | Executions per second |
---|---|
new Set | 2636222.0 Ops/sec |
uniq | 8867519.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Overview
The provided benchmark is designed to compare two approaches for removing duplicates from an array: using the Array.from(new Set)
method and using Lodash's uniq
function. The goal is to determine which approach is faster in terms of execution time.
Options Compared
There are two options being compared:
Set
object in JavaScript, which automatically removes duplicates from an array. It then converts the set back into an array using Array.from()
.uniq
. This function takes an array as input and returns a new array with duplicate elements removed.Pros and Cons
Library Used
In this benchmark, Lodash is used as a dependency to provide the uniq
function. Lodash is a popular JavaScript library that provides various utility functions for tasks like string manipulation, data transformation, and more.
Special JS Feature or Syntax
There are no special features or syntax used in this benchmark. The code only uses standard JavaScript syntax.
Other Alternatives
If you're interested in exploring other approaches to remove duplicates from an array, here are a few alternatives:
uniq
function.Benchmark Preparation Code
The benchmark preparation code is empty in this case, which means it doesn't perform any initial setup or teardown before running the benchmark.
Individual Test Cases
There are two test cases:
Array.from(new Set)
. It then returns the resulting array.uniq
function, which returns a new array with duplicates removed.Latest Benchmark Result
The latest benchmark result shows that Safari 16 on a Mac OS X 10.15.7 system can execute the uniq
test case approximately 5.3 times faster than the new Set
test case, with an execution rate of around 2636222 executions per second.