var array = 'test_'.repeat(49999).split('_');
array.push('findMe');
array.indexOf('findMe') !== 1
array.includes('findMe')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
indexOf | |
includes |
Test name | Executions per second |
---|---|
indexOf | 26352.1 Ops/sec |
includes | 433835.1 Ops/sec |
Let's break down the provided benchmark and its test cases to understand what's being tested.
Benchmark Purpose: The purpose of this benchmark is to compare the performance of three different methods for checking if an array contains a specific value:
array.indexOf()
array.includes()
array.some()
(with version 3, likely referring to the some()
method introduced in ECMAScript 2019)Options Compared:
indexOf()
: Returns the index of the first occurrence of the specified value, or -1 if not found.includes()
: Returns true if the specified value is present in the array, and false otherwise.indexOf()
for large arrays or exact matches.some()
: Returns true if at least one element in the array satisfies the provided callback function.indexOf()
or includes()
for exact matches.Library and Special Features: None mentioned, but it's worth noting that this benchmark assumes JavaScript environments that support these methods.
Now, let's discuss other alternatives for similar use cases:
Array.prototype.includes()
equivalent: If you're using an older JavaScript version that doesn't have the includes()
method, you can create a custom implementation using indexOf()
and a simple logic to check for presence.Considerations: When choosing between these methods, consider the following factors:
This benchmark provides a helpful comparison of three common methods for checking array presence, allowing users to evaluate their performance and choose the best approach for specific use cases.