Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex.test() vs str.match(regex) vs str.indexOf() with no matches
(version: 0)
Comparing performance of:
Regex.test() vs str.match(regex) vs str.indexOf();
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /href|src|title|cid\:/; 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:
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 break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark tests three different approaches to find occurrences of specific substrings in a given string: 1. `Regex.test()`: This method uses regular expressions to search for occurrences of the specified substring(s) in the input string. 2. `str.match(regex)`: This method returns an array of matches if the regular expression pattern is found at least once in the input string, or null otherwise. 3. `str.indexOf();`: This method searches for the first occurrence of a specific character or substring in the input string and returns its index. If no match is found, it returns -1. **Options Compared** The benchmark compares the performance of these three approaches: * `Regex.test()` + Pros: Fast and efficient for searching multiple substrings. + Cons: Can be slow for simple searches with a small number of occurrences, as it needs to compile the regular expression pattern first. * `str.match(regex)` + Pros: Simple and easy to use, especially when searching for multiple substrings. + Cons: May be slower than `Regex.test()` for large inputs or complex patterns, as it creates an array of matches. * `str.indexOf();` + Pros: Fast and efficient for simple searches with a single character or substring. + Cons: Slower than the other two approaches when searching for multiple substrings or complex patterns. **Library** The benchmark uses the `window.regex` variable, which is set to a regular expression pattern `/href|src|title|cid\\:/`. This library allows the user to define and reuse complex regular expressions across different tests. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. The code only uses standard JavaScript methods and variables, such as `const`, `let`, `window`, etc. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * `String.prototype.replace()`: This method can be used to search for multiple substrings by replacing them with an empty string. However, it may not be the most efficient approach. * `Array.prototype.findIndex()`: This method searches for the first occurrence of a specified element in an array and returns its index. It can be used as an alternative to `str.indexOf();`. * Using a dedicated string searching library or framework, such as Apache Commons Lang's `StringUtils` class. Keep in mind that the choice of approach depends on the specific requirements of your use case and performance considerations.
Related benchmarks:
RegexvsIndexOfvZ
Regex.test() vs str.match(regex) vs str.indexOf() v2.0
IndexOf vs Includes vs Reg StartsWith vs Reg Includes in string
RegEx.test vs. String.indexOf vs. String.match
Comments
Confirm delete:
Do you really want to delete benchmark?