Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String in array
(version: 0)
Comparing performance of:
indexOf vs includes
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const str = "2"; const arr = "1234".split('');
Tests:
indexOf
"1234".split('').indexOf('4') === -1
includes
"1234".split('').includes('2')
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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
47429076.0 Ops/sec
includes
52098864.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark test case and explain what is being tested. **Benchmark Definition** The provided JSON defines a JavaScript microbenchmark that tests two different methods: `indexOf` and `includes`. The benchmark definition itself is a JavaScript expression, which is executed to determine if it returns a specific value. In this case: * `\"1234\".split('').indexOf('4') === -1` checks if the string `"1234"` has no element equal to `'4'` in its split array. The expected result is `-1`, indicating that '4' is not found. * `\"1234\".split('').includes('2')` checks if the string `"1234"` contains the character `'2'`. The expected result is `true`. **Options Compared** The benchmark compares two approaches: 1. **indexOf**: This method returns the index of the first occurrence of a specified element in an array. If the element is not found, it returns `-1`. 2. **includes**: This method checks if an element with the value specified exists in an array. **Pros and Cons** * **indexOf** * Pros: * Faster for large arrays, as it can stop searching as soon as it finds a match. * Generally faster than `includes` for smaller arrays. * Cons: * Returns the index of the first occurrence of the element, which might not be useful if you're only interested in whether the element exists or not. * **includes** * Pros: * Returns a boolean value (true or false) indicating whether the element is present in the array, making it more intuitive for use cases where you just want to know if an element exists. * Can be faster than `indexOf` for smaller arrays due to its built-in loop and no need to find the index of the first occurrence. * Cons: * Generally slower than `indexOf` for large arrays, as it has to iterate through all elements. **Other Considerations** * The use of `split('')` is an optimization technique that converts the string into an array before performing the search. This can improve performance by allowing the browser's internal array methods to be used. * Both `indexOf` and `includes` are widely supported in modern browsers, but their behavior might differ slightly between different browsers or versions. **Library** In this case, no specific JavaScript library is being used beyond the built-in Array methods (`split`, `indexOf`, and `includes`). **Special JS Feature/Syntax** None mentioned. However, note that the use of template literals (e.g., `"\"1234\".split('').indexOf('4') === -1"`) is a feature introduced in ECMAScript 2015 (ES6) for creating string literals with embedded expressions. **Alternatives** Other alternatives to `indexOf` and `includes` include: * Manual array iteration using a loop * Using other searching algorithms, such as binary search * Using external libraries or functions that implement these operations (although this would likely introduce additional overhead) However, for most use cases, the built-in methods provided by modern JavaScript engines will be sufficient and performant enough.
Related benchmarks:
String in array
Array.from(string) vs string.split("")
Array split vs string slice
JSON.parse vs string.split small fixed array
Comments
Confirm delete:
Do you really want to delete benchmark?