var testArray = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'nineteen', 'twenty'];
testArray.includes('two');
testArray.includes('three');
testArray.includes('four');
testArray.includes('five');
testArray.includes('twenty');
testArray.includes('nineteen');
testArray.includes('eighteen');
testArray.includes('seventeen');
const testObject = {};
for (const key in testArray) testObject[key] = true;
testObject['one'];
testObject['two'];
testObject['three'];
testObject['four'];
const testObject = {};
for (const key in testArray) testObject[key] = true;
testObject['twenty'];
testObject['nineteen'];
testObject['eighteen'];
testObject['seventeen'];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array.includes at the beggining | |
array.includes at the end | |
Indexation THEN check at the beggining | |
Indexation THEN check at the end |
Test name | Executions per second |
---|---|
array.includes at the beggining | 3646802.0 Ops/sec |
array.includes at the end | 3585005.2 Ops/sec |
Indexation THEN check at the beggining | 913455.5 Ops/sec |
Indexation THEN check at the end | 916763.0 Ops/sec |
Let's break down the provided benchmark definition and test cases to understand what is being tested.
Benchmark Definition
The main goal of this benchmark is to compare the performance of two approaches:
testArray[0]
) versus using the includes()
method (e.g., testArray.includes('two')
).Test Cases
There are four test cases:
includes()
when it's called before accessing an element in the array.includes()
is called after accessing an element in the array.includes()
.includes()
.Libraries and Special Features
Pros and Cons of Different Approaches
Other Considerations
Alternatives
If you wanted to modify or extend this benchmark, here are some possible alternatives:
includes()
and array indexing on other browsers (e.g., Firefox, Edge).includes()
with other methods (e.g., indexOf()
, findIndex()
).By analyzing this benchmark definition and test cases, you can gain insights into the trade-offs between array indexing and array access methods, especially in situations where performance is critical.