Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
includes vs indexOf vs findIndex vs Regex
(version: 0)
Comparing performance of:
indexOf vs findIndex vs Regex vs includes
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = "aaaa"; var falseData = "bbbb"; var matchers = [ ...Array.from({length: 30}).map((_, i) => "dummy" + i), "aaaa", ...Array.from({length: 30}).map((_, i) => "dummyII" + i)]; var regex = new RegExp(`(^|\\.)(${matchers.join('|')})$`);
Tests:
indexOf
const res = matchers.indexOf(data) >= 0 const fRes = matchers.indexOf(falseData) >= 0
findIndex
const res = matchers.findIndex(key => key === data) >= 0 const fRes = matchers.findIndex(key => key === falseData) >= 0
Regex
const res = regex.test(data); const fRes = regex.test(falseData);
includes
const res = matchers.includes(data) const fRes = matchers.includes(falseData)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
indexOf
findIndex
Regex
includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
12152275.0 Ops/sec
findIndex
9860651.0 Ops/sec
Regex
34153348.0 Ops/sec
includes
12211410.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of four different string searching methods in JavaScript: `includes`, `indexOf`, `findIndex`, and regular expressions (`Regex`). The benchmark is designed to compare the execution speed of these methods on a specific test case, which involves searching for two strings ("aaaa" and "bbbb") within an array of 30 elements. **Benchmarked Methods** 1. **`includes`**: This method returns `true` if the specified value (in this case, "aaaa") is present in the array, or `false` otherwise. 2. **`indexOf`**: This method returns the index of the first occurrence of the specified value (in this case, "aaaa") in the array, or -1 if not found. 3. **`findIndex`**: This method returns the index of the first element in the array that satisfies the provided condition (in this case, `key => key === data`). If no element satisfies the condition, it returns -1. 4. **`Regex`**: This method uses a regular expression to search for the specified value ("aaaa") within the array. **Comparison and Considerations** The main difference between these methods lies in their approach: * `includes` and `indexOf` are both linear searches that iterate through the array until they find the target value. However, `indexOf` is generally faster because it returns the index of the first occurrence, which allows the browser to optimize the search. * `findIndex` is similar to `indexOf`, but it stops searching as soon as it finds the first match, which can be beneficial for performance-critical code paths. However, it may not work correctly if there are multiple matches with the same condition. * The `Regex` method uses a regular expression engine to search for the target value within the array. Regular expressions can provide more advanced matching capabilities but often come with a performance overhead. **Library and Special Features** The test case uses the built-in JavaScript `Array.prototype.includes`, `Array.prototype.indexOf`, and `Array.prototype.findIndex` methods, which are part of the ECMAScript standard. The regular expression engine is also part of the ECMAScript standard. There are no special features or syntax used in this benchmark that would be specific to a particular version of JavaScript or browser implementation. **Other Alternatives** If you were to implement these string searching methods from scratch, you could use: * **Linear search**: Iterate through the array until finding the target value. * **Trie-based search**: Prebuild a trie data structure and then perform lookups within it. * **Hashing-based search**: Use a hash function to map keys to indices in an array. Keep in mind that implementing these alternatives would likely result in slower performance compared to using the built-in `includes`, `indexOf`, and `findIndex` methods.
Related benchmarks:
Multimatch of Regex vs findIndex
RegEx vs Array.includes
RegEx vs Array.includes v2
RegEx.test vs Array.includes — fork 1
Comments
Confirm delete:
Do you really want to delete benchmark?