<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
var a = [1, 2, 3, 4, 5]
var b = [3, 4, 5, 6, 7]
var c = _.union(a, b)
const d = c.contains(a => a===3)
var c = new Set(a, b)
const d = c.has(3)
var c = new Set(a, b)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.union | |
Set() | |
Set() convert back to array |
Test name | Executions per second |
---|---|
_.union | 0.0 Ops/sec |
Set() | 2588013.0 Ops/sec |
Set() convert back to array | 2577717.0 Ops/sec |
Benchmark Overview
The provided benchmark is designed to compare the performance of Lodash's _.union
function with native JavaScript's Set()
data structure.
Options Compared
Two options are compared:
_union
function.Set
object in JavaScript, which is implemented in C++ and optimized for performance.Pros and Cons of Each Approach
Pros:
_union
function with two arrays as arguments.Cons:
Pros:
Set
object is implemented in C++ and optimized for performance, making it generally faster than any custom implementation.Cons:
Set
API is not as well-known as other JavaScript objects, requiring more effort to understand and use correctly.Library and Purpose
The library used in this benchmark is Lodash, a popular JavaScript utility library that provides a wide range of functions for tasks like string manipulation, object transformation, and more. In this case, the _union
function is used to combine two arrays into a single array with no duplicates.
Special JS Feature or Syntax
None mentioned in the provided code snippets.
Benchmark Preparation Code
The preparation code sets up two initial arrays a
and b
, which will be used as input for both benchmark tests. The Lodash library is loaded from a CDN using an HTML script tag.
Individual Test Cases
There are three test cases:
_union
function.Set()
data structure.Latest Benchmark Result
The latest benchmark result shows the execution times per second for each test case, measured on a Chrome 111 browser on a Windows Desktop platform:
Note that the results may vary depending on the specific test environment and hardware configuration.