<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.includes(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 | 3222084.8 Ops/sec |
Set() | 2211495.2 Ops/sec |
Set() convert back to array | 2234821.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is tested?
The provided JSON represents a benchmark test between three approaches:
Set
data structure in modern JavaScript engines, which can be used to store unique values.The benchmark tests the performance of these two approaches:
new Set(a, b)
(Test Name: "Set()")Options compared
The three options are:
Set
data structure in modern JavaScript engines, which can be used to store unique values.Pros and Cons
Here's a brief summary:
Special JS feature or syntax
None mentioned in the provided JSON. However, it's worth noting that modern JavaScript engines have many features and optimizations that can impact performance, such as:
Other alternatives
Some alternative approaches to consider when working with sets and unions:
Map
data structure instead of Set
, which can provide faster lookup times.Keep in mind that the best approach often depends on the specific use case and performance requirements.