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
"0123678".split('').indexOf(''+Math.floor((Math.random() * 10) + 1)) === -1
includes
"0123678".split('').includes(''+Math.floor((Math.random() * 10) + 1))
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):
Let's break down the provided JSON and explain what is tested on the benchmark, as well as the pros and cons of the different approaches. **Benchmark Definition** The benchmark definition is a simple JavaScript code snippet that tests two methods: `indexOf` and `includes`. Both methods are used to search for a specific character within an array of strings. The array contains a fixed string "0123678" split into individual characters using the `split` method. **Script Preparation Code** The script preparation code initializes two variables: * `str`: sets a constant variable to the string "2" * `arr`: splits the string "1234" into individual characters and assigns it to an array This setup ensures that the same data is used for both methods being tested, making the comparison between them more accurate. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark does not rely on any external HTML elements or interactions. **Library and Features Used** Neither of the methods (`indexOf` or `includes`) relies on a specific library. However, it's worth noting that `includes` was introduced in ECMAScript 2019 (ES10) as a replacement for `indexOf` when used with arrays containing unique values. The comparison between the two methods is testing which one is faster for this specific use case. **Options Compared** The options being compared are: * `indexOf`: an older method that returns `-1` if the specified value is not found in the array. * `includes`: a newer method introduced in ES10, which returns `false` if the specified value is not found in the array. **Pros and Cons of Different Approaches** Both methods have their pros and cons: * `indexOf`: + Pros: well-established, widely supported by older browsers and engines. + Cons: can be slower for large arrays due to its linear search algorithm. * `includes`: + Pros: designed specifically for array membership testing, faster than `indexOf`, and more readable syntax. + Cons: not as widely supported by older browsers and engines. **Other Considerations** It's essential to note that the benchmark is using a fixed string "0123678" split into individual characters. This simplification might not accurately represent real-world scenarios where strings can be of varying lengths or contain special characters. **Alternatives** If you're interested in exploring alternative methods, consider the following: * `some()`: another array method that returns true if at least one element matches the specified value. * `every()`: an array method that returns true if all elements match the specified value. * `findIndex()` and `findLastIndex()`: array methods that return the index of the first or last occurrence of a specified value. Keep in mind that these alternatives might not provide the same level of performance as `includes`, especially for large arrays.
Related benchmarks:
String in array
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?