var a = [
8123075259360718,
2180349503224798,
6686356311324193,
362184468266422,
2306757200330954,
4532178674851796,
8970950942018765
];
var b = new Set(a)
return a.includes(8123075259360718) ^ a.includes(7516472072139877) ^ a.includes(362184468266422);
return b.has(8123075259360718) ^ b.has(7516472072139877) ^ b.has(362184468266422);
return (a.indexOf(8123075259360718) > -1) ^ (a.indexOf(7516472072139877) > -1) ^ (a.indexOf(362184468266422) > -1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
lookup | |
indexof |
Test name | Executions per second |
---|---|
includes | 10296805.0 Ops/sec |
lookup | 1561226496.0 Ops/sec |
indexof | 10627023.0 Ops/sec |
Overview
The provided JSON represents a JavaScript benchmark test case on the MeasureThat.net website. The test case compares three different approaches to check if a specific value exists in an array: using array.includes()
, Set
lookup (b.has()
), and array.indexOf()
.
Benchmark Definition
The benchmark definition is as follows:
a
with six numeric values.Set
object b
from the array a
.Three individual test cases are defined, each using one of the three approaches to check if three specific values (8123075259360718
, 7516472072139877
, and 362184468266422
) exist in the set b
or array a
. The test cases return a boolean value indicating whether all three values exist.
Options Compared
The options compared are:
array.includes()
: This method checks if a specified value exists in an array. It returns true
if the value is found, and false
otherwise.Set
lookup (b.has()
): This method checks if a specified value exists in a Set
object. It returns true
if the value is present, and false
otherwise.array.indexOf()
: This method returns the index of the first occurrence of a specified value in an array. If the value is not found, it returns -1
.Pros and Cons
Here are some pros and cons for each approach:
array.includes()
:Set
lookup or indexOf()
for large arrays, as it scans the array from start to end.Set
lookup (b.has()
):array.includes()
and can be more efficient for large sets.Set
object, which may incur additional memory overhead. Not all browsers support Set
objects.array.indexOf()
:array.includes()
for small arrays or when using cached results.-1
if the value is not found.Library and Special JS Features
The test case uses a Set
object, which is a built-in JavaScript object that stores unique values. The b.has()
method is used to perform lookup operations on the set.
No special JavaScript features or syntax are required for this benchmark.
Other Alternatives
Alternative approaches could include:
array.prototype.includes()
instead of array.includes()
, which uses an iterator for better performance.Array.from()
and includes()
in combination with Set
lookup.Map
object, to store and retrieve values.However, these alternatives may not provide significant performance benefits or changes to the benchmark results.