Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes in array by miette
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'.split(' ')
Tests:
IndexOf
arr.indexOf('tempor')
Includes
arr.includes('tempor')
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its options. **Benchmark Overview** The benchmark measures the performance difference between using `indexOf` and `includes` methods in JavaScript for searching an array. The test case uses a specific string that is split into an array, which contains the word "tempor". **Options Compared** Two options are compared: 1. `arr.indexOf('tempor')`: This method returns the index of the first occurrence of the specified value (in this case, 'tempor'). If the value is not found, it returns -1. 2. `arr.includes('tempor')`: This method returns a boolean indicating whether the array includes the specified value ('tempor' in this case). **Pros and Cons** * **indexOf**: + Pros: Generally faster and more efficient, especially for large arrays, since it only checks each element individually until it finds a match. + Cons: Returns an index (which can be negative if not found), which may require additional processing to handle. * **includes**: + Pros: More readable and concise, as it returns a boolean value directly. Also, more modern and widely supported. + Cons: May be slower for very large arrays, since it needs to iterate over the entire array. **Library** There is no specific library mentioned in the benchmark definition or test cases. The `includes` method is a built-in JavaScript method that was introduced in ECMAScript 2015 (ES6). **Special JS Feature/Syntax** The benchmark uses a modern JavaScript feature, specifically `includes`, which was added to the language in ES6. **Other Considerations** When choosing between `indexOf` and `includes`, consider the following: * If you need to find the index of an element in an array, use `indexOf`. * If you simply need to determine if an element is present in an array, using a boolean return value, use `includes`. As for alternative approaches, other options could include: * Using `findIndex` or `find`, which can be slower than `indexOf` and `includes`, but provide more flexibility. * Implementing your own custom search function, which can be optimized for performance but may add complexity. **Additional Notes** For this specific benchmark, the results indicate that `includes` is slightly faster than `indexOf` in Chrome 110 on a Linux desktop. However, the actual performance difference may vary depending on the specific use case and environment.
Related benchmarks:
String indexOf vs includes
IndexOf vs Includes str
IndexOf > -1 vs Includes in string
IndexOf vs Includes in string as boolean
IndexOf vs Includes in string 2
Comments
Confirm delete:
Do you really want to delete benchmark?