var a = Array.from({ length: 100 }, (_, index) => index);
var b = new Set(a)
return a.includes(69)
return b.has(69)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
lookup |
Test name | Executions per second |
---|---|
includes | 18741644.0 Ops/sec |
lookup | 19096920.0 Ops/sec |
Let's break down what's being tested in the provided JSON benchmark.
Benchmark Overview
The benchmark is comparing two different approaches to check if an element exists in an array: Array.prototype.includes()
and Set.prototype.has()
. The test case consists of two individual tests:
Script Preparation Code
The script preparation code creates an array a
with 100 elements, each incrementing from 0 to 99, using the Array.from()
method and a callback function that returns the current index. A new Set
object b
is created by passing the array a
as its initializer.
Options Compared
The two options being compared are:
Pros and Cons
Array.prototype.includes()
:Set.prototype.has()
:Library Used
In this benchmark, the Set
object is used, which is a built-in JavaScript object that allows you to store unique values. The purpose of using a set here is to compare its has()
method with the Array.prototype.includes()
method.
Special JS Feature or Syntax (Not Applicable in This Case)
There are no special JavaScript features or syntaxes being tested or utilized in this benchmark.
Other Alternatives
For membership testing in an array, other alternatives include:
indexOf()
: Returns the index of the first occurrence of the specified element. If the element is not found, it returns -1.forEach()
with a callback function that checks for the presence of the element.filter()
to create a new array with only the elements that match the condition.For membership testing in a set, alternative approaches include:
Keep in mind that these alternatives may have different performance characteristics compared to the Set.prototype.has()
method.