Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array IndexOf vs includes
(version: 0)
Comparing performance of:
Includes vs IndexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array1 = [0,1,3,532,32,3576,745,1235,6432,31265,654,43212,354,4667,875,999,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991]
Tests:
Includes
array1.indexOf(31265) > -1
IndexOf
array1.includes(31265)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Includes
IndexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
68798944.0 Ops/sec
IndexOf
71251960.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmarking test on MeasureThat.net. The benchmark compares the performance of two methods to check if an element exists in an array: `indexOf` and `includes`. **What is being tested?** Two individual test cases are defined: 1. **Includes**: Verifies if `array1.includes(31265)` returns a boolean value indicating whether 31265 is present in the array. 2. **IndexOf**: Verifies if `array1.indexOf(31265)` returns an index value (or -1 if not found) indicating the position of 31265 in the array. **Options compared** The two methods are compared in terms of their execution time, with the goal of identifying which one is faster for this specific use case. **Pros and Cons of each approach** * **`includes` method** + Pros: - More readable and concise code. - Less prone to off-by-one errors or edge cases. + Cons: - May be slower than `indexOf` due to the overhead of string comparison. * **`indexOf` method** + Pros: - Can return an index value, which might be useful in certain situations. + Cons: - More verbose code. - Prone to off-by-one errors or edge cases if not used carefully. **Library usage** There is no explicit library mentioned in the benchmark definition. However, `array1` is a JavaScript array created using the default `var` syntax, which is a part of the ECMAScript standard. **Special JS feature/syntax** The test does not explicitly use any special JavaScript features or syntax that would require additional explanation. Both methods are widely supported and should work in most modern browsers. **Other alternatives** If you wanted to implement this benchmark yourself, you could create an array `array1` with a large number of elements and then measure the execution time of both `includes` and `indexOf` methods using a timer or profiling tool. You would also need to consider any platform-specific differences in behavior or performance. In JavaScript, alternative approaches for similar use cases might include: * Using `includes()` method on other data structures like strings or sets. * Implementing custom search algorithms for arrays or other data structures. * Using optimized libraries or frameworks that provide faster array searching mechanisms.
Related benchmarks:
IndexOf vs Includes vs lodash includes 1231
indexOf VS includes in JavaScript Array
set.has vs. array.includes vs array.indexOf (string values) - 800 ids
array indexOf vs includes vs some w/ largeish array
Comments
Confirm delete:
Do you really want to delete benchmark?