Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.indexOf vs Array.includes
(version: 0)
Comparing performance of:
indexOf Speed vs includes Speed
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
indexOf Speed
const sampleArray = ['a','b','c','d','e','f']; let indexOf = sampleArray.indexOf('b');
includes Speed
const sampleArray = ['a','b','c','d','e','f']; let includes = sampleArray.includes('b');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexOf Speed
includes Speed
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 YaBrowser/25.2.0.0 Safari/537.36
Browser/OS:
Yandex Browser 25 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf Speed
74244072.0 Ops/sec
includes Speed
72087824.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Definition** The provided JSON represents a benchmark for comparing the performance of `Array.indexOf` and `Array.includes` methods in JavaScript. **What is being tested?** Two individual test cases are defined: 1. **indexOf Speed**: This test case measures the execution speed of the `indexOf` method on an array containing six elements. 2. **includes Speed**: This test case measures the execution speed of the `includes` method on the same array. **Options compared** The two methods being compared have different performance characteristics: * **indexOf**: Returns the index of the first element in the array that matches the specified value, or -1 if no match is found. It has to iterate through the entire array until it finds a match. * **includes**: Returns `true` if an element with the specified value exists in the array, and `false` otherwise. It uses a more efficient algorithm than `indexOf`, as it can stop searching as soon as it finds a match. **Pros and Cons** * **indexOf:** + Pros: - Simple to implement - Works well for small arrays or arrays with unique elements + Cons: - Has to iterate through the entire array, which makes it slower than `includes` for large arrays - May be less efficient due to its use of a linear search algorithm * **includes:** + Pros: - More efficient, as it can stop searching once a match is found - Less memory-intensive, as it doesn't require storing the index of the matched element + Cons: - May be slower than `indexOf` for very small arrays or arrays with unique elements **Library and Purpose** There are no external libraries used in this benchmark. **Special JavaScript Feature or Syntax** None mentioned. Both methods are standard JavaScript methods. **Other Considerations** When choosing between these two methods, consider the following: * If you need to find a specific element in an array and don't care about its index, use `includes`. * If you need to find the index of a specific element in an array, use `indexOf`. **Alternatives** Other alternatives for comparing string matching algorithms could be: 1. Using a regular expression engine like RegExp or using the String.prototype.match() method. 2. Comparing the performance of other string matching algorithms like KMP (Knuth-Morris-Pratt) or Rabin-Karp. However, these alternatives may not be as straightforward to implement and compare as `indexOf` and `includes`.
Related benchmarks:
IndexOf vs Includes
JavaScript Benchmark: includes vs indexOf
IndexOf vs Includes in array
Array find with indexOf vs includes
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?