<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var a = [1, 2, 3, 4, 5]
var b = [3, 4, 5, 6, 7]
var c = _.union(a, b)
var c = [new Set([a, b])]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.union | |
Set() |
Test name | Executions per second |
---|---|
_.union | 696958.0 Ops/sec |
Set() | 423077.2 Ops/sec |
I'd be happy to help you understand the JavaScript microbenchmark on MeasureThat.net.
Benchmark Overview
The benchmark compares two approaches for performing a union operation: Lodash's _.union()
function and native JavaScript's Set
constructor.
Script Preparation Code
The script preparation code sets up two arrays, a
and b
, which contain elements to be used in the union operation. The arrays are:
var a = [1, 2, 3, 4, 5]
var b = [3, 4, 5, 6, 7]
Html Preparation Code
The HTML preparation code includes a script tag that loads Lodash version 4.17.5 from a CDN.
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Benchmark Definition
The benchmark definition specifies two test cases:
_.union
: This test case uses Lodash's _.union()
function to perform the union operation.Set()
: This test case uses native JavaScript's Set
constructor to perform the union operation.Comparison of Approaches
Let's compare the two approaches:
Lodash's _
.union():
Pros:
Cons:
Native JavaScript's Set
:
Pros:
Cons:
for...of
loops)Library: Lodash
Lodash is a popular JavaScript utility library developed by Isaac Schlueter. It provides a wide range of functional programming tools and helpers for tasks such as array manipulation, object transformation, and more.
In this benchmark, Lodash's _.union()
function is used to perform the union operation. The implementation is likely optimized for performance, which explains its relatively fast execution speeds in the benchmark.
Special JS Feature or Syntax
This benchmark does not utilize any special JavaScript features or syntax beyond standard ES6/ES7 features, such as template literals, arrow functions, and for...of
loops. If it did, I'd be happy to explain them further!
Other Alternatives
If you're looking for alternatives to Lodash's _.union()
function or native JavaScript's Set
constructor, here are a few options:
Array.prototype.union()
: A polyfilled implementation of the union()
method that can be used with arrays.Set
constructor:Array.prototype.sort()
and Array.prototype.unique()
: These methods can be used to create a set-like behavior for arrays.I hope this explanation helps you understand the JavaScript microbenchmark on MeasureThat.net!