<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var a = [ 'a', 'b', 'c' ];
var b = [ 'b', 'd', 'a', 'e', 'f' ];
var c = _.union(a, b);
var a = [ 'a', 'b', 'c' ];
var b = [ 'b', 'd', 'a', 'e', 'f' ];
var c = [new Set([a ,b])];
var a = [ 'a', 'b', 'c' ];
var b = [ 'b', 'd', 'a', 'e', 'f' ];
var c = _.uniq([a, b]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash union | |
new Set | |
lodash uniq spread |
Test name | Executions per second |
---|---|
lodash union | 7302797.0 Ops/sec |
new Set | 9213970.0 Ops/sec |
lodash uniq spread | 12867453.0 Ops/sec |
Let's break down the provided benchmark and its test cases.
Overview
The benchmark is designed to compare the performance of three approaches for removing duplicates from an array: Lodash's union
function, creating a new Set using the spread operator (new Set([...a, ...b])
), and Lodash's uniq
function with the spread operator (_.uniq([...a, ...b])
). The benchmark aims to determine which approach is the most efficient.
Test Cases
There are three test cases:
union
function to combine two arrays and remove duplicates.uniq
function with the spread operator to remove duplicates from an array.Options Compared
The benchmark is comparing three options:
union
functionnew Set([...a, ...b])
)uniq
function with the spread operator (_.uniq([...a, ...b])
)Pros and Cons
Here are some pros and cons of each approach:
Libraries
Two libraries are used in this benchmark:
Special JS Features or Syntax
The benchmark uses modern JavaScript features, such as:
[...a, ...b]
)Overall, the benchmark provides a good comparison of three approaches for removing duplicates from an array and highlights the pros and cons of each approach.
Other Alternatives
If you're looking for alternative ways to remove duplicates from an array, here are some options:
Array.prototype.filter()
: a.filter((x, i) => a.indexOf(x) === i)
Array.prototype.reduce()
: a.reduce((acc, x) => acc.includes(x) ? acc : [...acc, x])
Set
objects: new Set(a).size
JSON.parse(JSON.stringify([...a, ...b]))
: This approach assumes that the elements in the array are JSON serializable.Keep in mind that each of these alternatives has its own trade-offs and may not be as efficient or easy to use as the approaches tested in this benchmark.