Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex.test() vs str.match(regex) vs str.indexOf() v2.0
(version: 2)
Comparing performance of:
Regex.test() vs str.match(regex) vs str.indexOf();
Created:
6 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /href|src|title|cid\:/; //window.data = 'title=https://gitlab.zivver.org/zivver/zivver-webapp/merge_requests/150'; window.data = 'At mus porta risus sed vulputate proin velit nulla platea aliquam torquent lacus elit magna facilisis adipiscing cursus fermentum purus gravida rhoncus eget viverra tempus pellentesque dignissim curabitur condimentum morbi';
Tests:
Regex.test()
const regex = window.regex; const data = window.data; regex.test(data);
str.match(regex)
const regex = window.regex; const data = window.data; data.match(regex);
str.indexOf();
const data = window.data; data.indexOf('href') === -1 && data.indexOf('src') === -1 && data.indexOf('title') === -1 && data.indexOf('cid:') === -1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Regex.test()
str.match(regex)
str.indexOf();
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex.test()
6665609.0 Ops/sec
str.match(regex)
6525710.0 Ops/sec
str.indexOf();
11777357.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Overview** The provided JSON represents a JavaScript microbenchmark that compares three different approaches to test for the presence of certain substrings in a string: `Regex.test()`, `str.match(regex)`, and `str.indexOf()` with version 2.0 (Note: The exact version number is not specified, but I'll assume it's version 2.0). **Options Compared** 1. **Regex.test()**: This method uses the `test` function of a regular expression object to test if a string matches a given pattern. 2. **str.match(regex)**: This method uses the `match` function of a string to search for a given pattern and returns an array containing the matched substrings or null if no match is found. 3. **str.indexOf()` v2.0**: This method uses the `indexOf` function of a string to find the position of a specified value in a string. **Pros and Cons** 1. **Regex.test()** * Pros: + Can be used with more complex patterns, including capturing groups. + Faster than `str.match()` for simple cases. * Cons: + More difficult to read and maintain due to the complexity of regular expressions. + May have performance issues if the pattern is too complex or uses backtracking. 2. **str.match(regex)** * Pros: + Simple to understand and use, especially for simple patterns. + Returns an array of matched substrings, which can be useful in certain scenarios. * Cons: + Slower than `Regex.test()` for simple cases due to the overhead of creating a new regular expression object. 3. **str.indexOf() v2.0** * Pros: + Fast and efficient, especially for large strings. * Cons: + Only searches for exact matches (i.e., no regex pattern matching). + Returns -1 if the value is not found, which may be considered an error in some cases. **Library/Functionality Used** * `str.match(regex)`: Uses the `match` function of a string to search for a given pattern. * `str.indexOf() v2.0`: Uses the `indexOf` function of a string to find the position of a specified value in a string. **Special JS Feature/Syntax** None mentioned explicitly, but regular expressions (regex) are used extensively in this benchmark. Regular expressions can be complex and may require a good understanding of regex syntax and semantics to read and maintain. **Other Alternatives** If you need to perform substring matching, you might also consider using: * `str.includes()`: A more modern method that checks if a string includes a specified value. * `Array.prototype.some()`: An array method that tests each element in an array to see if it matches a condition. * `RegExp.test()` (similar to `Regex.test()`): Some modern browsers support the `test` function on regular expressions, which provides similar functionality. Keep in mind that these alternatives might not be as suitable for every use case, and the specific requirements of your project should guide your choice.
Related benchmarks:
RegexvsIndexOfvZ
Regex.test() vs str.match(regex) vs str.indexOf() with no matches
.startsWith vs .indexOf
JS Regex vs .startsWith vs .indexOf and lastIndexOf
Comments
Confirm delete:
Do you really want to delete benchmark?