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('4')
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. **What is tested?** The provided benchmark measures the performance difference between two methods: `indexOf` and `includes`. Both methods are used to search for a specific substring within an array. The test case uses a simple string literal "1234" as the input, which is split into individual characters using the `.split('')` method. **Options compared** Two options are compared: 1. **`indexOf`**: This method returns the index of the first occurrence of the specified value within the array, or -1 if not found. 2. **`includes`**: This method returns a boolean indicating whether the specified value is present in the array, without returning its index. **Pros and cons of each approach** * `indexOf`: + Pros: Generally faster, as it can terminate early if the substring is not found. + Cons: Returns an index, which may not be useful for all use cases. If the substring is not found, returns -1, which can lead to errors in some applications. * `includes`: + Pros: Simpler and more intuitive, as it returns a boolean value directly. + Cons: May be slower than `indexOf`, especially for large arrays or multiple iterations. **Library usage** Neither of the tested methods uses any external libraries. The `.split('')` method is a built-in JavaScript function, and both `indexOf` and `includes` are also part of the standard JavaScript API. **Special JS feature or syntax** There are no special features or syntaxes being tested in this benchmark. **Other alternatives** For array search operations, other alternatives might include: * Using `forEach` and checking for equality: `arr.forEach((val) => val === '4');` * Using a regular expression: `/4/.test(arr)` However, these alternatives may have performance implications compared to the `indexOf` and `includes` methods. **Benchmark preparation code explanation** The script preparation code defines two variables: 1. `const str = "2";`: An empty string literal. 2. `const arr = "1234".split('');`: The input array, created by splitting a string into individual characters using the `.split('')` method. Note that the `str` variable is not actually used in the benchmark; it's likely included as part of the setup code for the script preparation. The HTML preparation code is empty, indicating that no specific HTML structure or setup is required for this benchmark.
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?