var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
var result = array.includes('d');
var result = array.indexOf('d') > -1;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.includes() | |
Array.indexOf() |
Test name | Executions per second |
---|---|
Array.includes() | 8836988.0 Ops/sec |
Array.indexOf() | 7822926.5 Ops/sec |
Let's break down what's being tested in this benchmark.
What is being tested?
The benchmark compares the performance of two methods: array.includes()
and array.indexOf()
. Both methods are used to search for a specific value ("d") within an array.
Options compared:
array.includes('d')
: This method checks if the specified value ('d'
) exists in the array using the includes()
method. It returns a boolean value indicating whether the value is present or not.array.indexOf('d') > -1
: This method searches for the specified value ('d'
) in the array and returns its index if found, or -1
if not found.Pros and Cons of each approach:
array.includes('d')
:array.indexOf('d') > -1
:> -1
) to get the boolean result. The syntax is more verbose.Library used in the test case:
None explicitly mentioned, but array
is a built-in JavaScript array object.
Special JS feature or syntax:
There are no specific special features or syntaxes being tested in this benchmark. However, it's worth noting that both methods rely on the JavaScript engine's implementation of arrays and the includes()
method.
Other alternatives:
For searching for values in an array, other methods might include:
for
loop to iterate through the array.some()
method, which returns true
if at least one element matches the condition.findIndex()
method, which returns the index of the first element that matches the condition.Keep in mind that these alternatives might have different performance characteristics and use cases compared to the includes()
and indexOf()
methods.