Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match vs. IndexOf
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.match vs String.indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Hello world!"; var regex = /Hello/;
Tests:
RegEx.test
regex.test(string);
String.includes
string.includes("Hello");
String.match
string.match("Hello");
String.indexOf
string.indexOf("Hello");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
String.match
String.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
31394680.0 Ops/sec
String.includes
1257394560.0 Ops/sec
String.match
15864405.0 Ops/sec
String.indexOf
1267748480.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Overview** The benchmark measures the performance of three different ways to search for a substring or regular expression in a string: `string.includes()`, `String.match()`, `regex.test()`, and `indexOf()`. **Options Compared** 1. **`string.includes(string)`**: This method uses the "includes" algorithm, which is optimized for case-insensitive matching. 2. **`String.match(regexp)`**: This method uses a regular expression engine to search for a pattern in the string. It can be used with or without capturing groups. 3. **`regex.test(string)`**: This method tests if a given string matches a regular expression. It's similar to `String.match()`, but returns a boolean value instead of an array of matches. 4. **`string.indexOf(string)`**: This method uses the "indexOf" algorithm, which is optimized for case-sensitive matching. **Pros and Cons** 1. **`string.includes()`**: * Pros: Fast, efficient, and case-insensitive by default. * Cons: May not be as flexible as other options (e.g., can't use regular expressions). 2. **`String.match()`**: * Pros: Can be used with regular expressions, supports capturing groups. * Cons: May be slower than `string.includes()`, and the overhead of creating a regex engine can slow down performance. 3. **`regex.test()`**: * Pros: Similar to `String.match()`, but returns a boolean value instead of an array of matches. * Cons: Same as `String.match()`, with the added overhead of testing for match instead of capturing groups. 4. **`string.indexOf()`**: * Pros: Fast and efficient, case-sensitive by default. * Cons: May not be as flexible as other options (e.g., can't use regular expressions). **Library/Feature Considerations** None mentioned in the benchmark definition. No special JavaScript features or syntax are being tested in this benchmark. **Alternative Approaches** Other ways to search for a substring or regular expression in a string include: * Using `indexOf()` with an offset parameter * Using a custom implementation using bitwise operations and loop * Using a library like jQuery's `.grep()` method (not mentioned in the benchmark definition) Keep in mind that this benchmark is specifically measuring the performance of these four options, so other approaches may not be included for comparison.
Related benchmarks:
RegEx.test vs. String.includes vs. String.indexOf
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test (with inline regex) vs. String.includes vs. String.match
Long regex test vs string includes
Longer regex test vs string includes
Comments
Confirm delete:
Do you really want to delete benchmark?