var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
var result = array.includes('d');
for (let i = 0; i < array.length; i++) {
if (array[i] === 'd') {
var result = true;
break;
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.includes | |
For |
Test name | Executions per second |
---|---|
Array.includes | 104618424.0 Ops/sec |
For | 48081284.0 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Definition JSON
The provided JSON represents the overall benchmark, which is named "Foreach vs Array.includes". The description field is empty, and there are two script preparation codes:
var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
is used for both test cases.Individual Test Cases
There are two individual test cases:
includes()
method call on the defined array, searching for the string 'd'
.var result = array.includes('d');
for
loop to iterate over the array and check if the current element is equal to 'd'
. The loop breaks as soon as it finds the match.for (let i = 0; i < array.length; i++) {
if (array[i] === 'd') {
var result = true;
break;
}
}
Options Compared
The two test cases compare the performance of:
includes()
methodfor
loop with a conditional statementPros and Cons
Here's a brief summary of the pros and cons of each approach:
for
loop due to method call overheadLibrary
The includes()
method uses the ECMAScript standard's built-in implementation of the Array.prototype.includes()
method, which is typically implemented using a binary search algorithm. The browser implements this method efficiently.
Special JS Feature/Syntax
There are no special JavaScript features or syntax used in these benchmark definitions.
Alternatives
If you wanted to create an alternative benchmark, you could consider:
includes()
method callforEach()
, map()
, or reduce()
These alternatives would allow you to explore various aspects of array manipulation and iteration in JavaScript.