Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.matchAll vs. String.indexOf vs. String.match
(version: 0)
Comparing performance of:
RegEx.matchAll vs String.indexOf vs String.match
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Hello world!"; var regex = /Hello|world/g;
Tests:
RegEx.matchAll
string.matchAll(regex);
String.indexOf
string.indexOf("Hello"); string.indexOf("world");
String.match
string.match("Hello"); string.match("wello");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegEx.matchAll
String.indexOf
String.match
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/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.matchAll
6302297.0 Ops/sec
String.indexOf
67158568.0 Ops/sec
String.match
1653990.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Benchmark Overview** The test compares three different approaches to find occurrences of a pattern in a string: 1. `RegEx.matchAll(regex)` 2. `String.indexOf("Hello");` followed by `string.indexOf("world")` 3. `String.match("Hello");` followed by `string.match("wello")` **Approach 1: RegEx.matchAll(regex)** This approach uses a regular expression (`regex`) to search for all occurrences of the pattern in the string. The `matchAll()` method returns an iterator that yields all matches. Pros: * Efficient and flexible, as it can match any part of the string. * Can handle multiple captures (i.e., find all occurrences). Cons: * May be slower than other approaches if the pattern is complex or the string is large. * Can be more difficult to read and understand for developers without experience with regular expressions. **Approach 2: String.indexOf("Hello"); string.indexOf("world")** This approach uses two separate calls to `indexOf()` to find occurrences of "Hello" and then another call to find occurrences of "world". This approach is a simple, linear search. Pros: * Easy to understand and implement. * Can be faster than regular expressions for small strings or simple patterns. Cons: * May not work correctly if the pattern spans multiple words (as in this example). * Requires two separate calls to `indexOf()`, which can be less efficient than using a single method like `matchAll()`. **Approach 3: String.match("Hello"); string.match("wello")** This approach uses two separate calls to `match()` to find occurrences of "Hello" and then another call to find occurrences of "wello". This approach is similar to the previous one but uses the `match()` method instead. Pros: * May be faster than regular expressions for small strings or simple patterns. * Easy to understand and implement. Cons: * Similar limitations as Approach 2, may not work correctly if the pattern spans multiple words. * Requires two separate calls to `match()`, which can be less efficient than using a single method like `matchAll()`. **Libraries and Special JS Features** None of the approaches explicitly use any libraries or special JavaScript features. The `RegEx` object is part of the built-in `RegExp` API, which is not included in the benchmark results. However, it's worth noting that the benchmark uses a regular expression with a global flag (`/Hello|world/g`), which allows it to find all occurrences of the pattern in the string. This syntax requires some knowledge of regular expressions to understand. **Alternatives** Other approaches that could be used for this benchmark include: * Using a library like `stringsimilarity` or `string-similarity-js`, which provide optimized algorithms for searching strings. * Using a more advanced regular expression engine, such as PCRE (Perl-Compatible Regular Expressions), which may offer better performance and flexibility. Overall, the benchmark provides a good starting point for understanding the relative performance of different approaches to finding occurrences in a string. However, the choice of approach will depend on the specific requirements and constraints of the use case.
Related benchmarks:
RegEx.test vs String.includes vs String.indexOf
RegEx.test vs. String.includes vs. String.match vs String.indexOf
RegEx.test vs. String.includes vs. String.indexOf
Reuse Global Regex? RegEx.test vs. String.match vs. String.search
RegEx.test vs. String.includes vs. String.match vs. IndexOf
Comments
Confirm delete:
Do you really want to delete benchmark?