var a = Array(10000).fill(undefined).map((_, i) => i);
var b = a.reduce((acc, el) => { acc[el] = true; return acc }, {});
var c = new Set(a);
var d = new Map(a.map(el => [el, true]));
return a.includes(9)
return b['9']
return c.has(9)
return d.get(9)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
hash | |
set | |
map |
Test name | Executions per second |
---|---|
includes | 25697368.0 Ops/sec |
hash | 44145224.0 Ops/sec |
set | 45429340.0 Ops/sec |
map | 43847180.0 Ops/sec |
Let's break down the provided benchmark json and explain what is being tested, compared, and its pros and cons.
Benchmark Definition
The benchmark definition represents four different ways to check if a value (in this case, 9) exists in an array:
a.includes(9)
: Checks if the array a
contains the value 9 using the includes()
method.b['9']
: Accesses the value associated with key 9 in the object b
.c.has(9)
: Checks if the set c
contains the value 9 using the has()
method.d.get(9)
: Retrieves the value associated with key 9 in the map d
.Comparison
These four methods are being compared to determine which one is the fastest.
Pros and Cons of each approach:
a.includes(9)
:b['9']
:Map
).c.has(9)
:d.get(9)
:Special JS feature or syntax:
None of the provided methods use special JavaScript features or syntax that would require additional explanation.
Alternative approaches:
Other alternatives to these methods could include:
Array.prototype.indexOf()
instead of includes()
.However, these alternative approaches are not being tested in this benchmark.
Other considerations:
Map
implementation in JavaScript is still evolving and may not be optimized for all use cases.I hope this explanation helps! Let me know if you have any further questions.