Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript Benchmark: includes vs indexOf
(version: 0)
Comparison of JavaScript Array.includes() and Array.indexOf()
Comparing performance of:
IndexOf vs Includes
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from({ length: 1000 }, (_, idx) => ++idx)
Tests:
IndexOf
arr.indexOf(500)
Includes
arr.includes(500)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
IndexOf
Includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
2347350.0 Ops/sec
Includes
1499478.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided benchmark. **What is being tested?** The test compares two JavaScript array methods: `Array.indexOf()` and `Array.includes()`. Both methods are used to find the index of a specific value within an array. However, there's a crucial difference between them: * `Array.indexOf()` returns the index of the first occurrence of the specified value in the array. If the value is not found, it returns `-1`. * `Array.includes()` also finds the index of the first occurrence of the specified value, but if the value is not found, it returns `false` (or `undefined` in some versions of JavaScript). **Options compared** The test compares two options: 1. **Linear search**: Both `indexOf()` and `includes()` use a linear search approach, which involves iterating through each element in the array until a match is found. 2. **Early exit**: The main difference between the two methods lies in how they handle cases where the value is not found in the array. `indexOf()` returns `-1` immediately when it can't find the value, while `includes()` continues to iterate and returns `false` only after the search is complete. **Pros and cons of each approach** * **Linear search (indexOf())**: + Pros: Faster for large arrays since it stops searching as soon as it finds a match. + Cons: May perform poorly on smaller arrays due to the overhead of searching, and can be slow if the value is not found early in the array. * **Early exit (includes())**: + Pros: Can be faster overall because it avoids the overhead of continuing to search after finding a non-match. + Cons: May be slower for small arrays since it has to continue searching even when it knows the value won't be found. Other considerations: * In modern JavaScript engines, `Array.includes()` is often implemented in native code, which can make it faster than `indexOf()`. However, this difference is not always significant and may depend on the specific use case. * The test results don't account for other factors that might affect performance, such as array caching or caching of intermediate results. **Library usage** In this benchmark, no libraries are explicitly used. Both methods are part of the built-in JavaScript API. **Special JS features** There is no special JS feature being tested in this benchmark. The focus is solely on comparing the performance of two different array methods. Now, regarding alternative approaches: * **Other array searching methods**: There are other array methods available in JavaScript that can be used for searching, such as `Array.prototype.findIndex()`, which returns the index of the first element that satisfies a provided function. However, these methods are less commonly used and may not offer significant performance improvements over `indexOf()` or `includes()`. * **Native implementations**: In some cases, you might want to use native implementations like `indexOf64()` (available in older JavaScript engines) or native arrays with optimized searching algorithms. * **Cache-based approaches**: If you need to search a large array multiple times, you could consider caching the results of previous searches to avoid redundant computations. However, this approach requires careful consideration of cache invalidation and memory usage. In summary, the benchmark provides a straightforward comparison between `Array.indexOf()` and `Array.includes()`, highlighting their differences in performance and handling cases where the value is not found in the array.
Related benchmarks:
Array.includes() vs Array.indexOf()
IndexOf vs includes js (idanlevi1)
IndexOf vs Includes array of numbers
Array find with indexOf vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?