Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.test vs .exec vs .match
(version: 0)
Simple regexs benchmarks
Comparing performance of:
re.test(str) vs re.exec(str) vs str.match(re)
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
re.test(str)
var re = /[$£]/ var match = re.test("$");
re.exec(str)
var re = /[$£]/ var match = re.exec("$");
str.match(re)
var re = /[$£]/ var match = "$".match(re);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
re.test(str)
re.exec(str)
str.match(re)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
re.test(str)
59332028.0 Ops/sec
re.exec(str)
41876276.0 Ops/sec
str.match(re)
40291808.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this benchmark. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches for searching regular expressions (regex) in strings: `re.test()`, `re.exec()`, and `str.match()`. The tests are simple, using a regex that matches either the dollar sign ($) or the pound sign (£). **Approach Options** We have three main options to compare: 1. **`re.test(str)`**: This method returns `true` if the string matches the regex pattern, and `false` otherwise. 2. **`re.exec(str)`**: This method executes the regex pattern on the string and returns an array containing match results if there's a match, or null if not. 3. **`str.match(re)`**: This method searches for any occurrence of the regex pattern in the string. **Pros and Cons** Here are some pros and cons for each approach: 1. **`re.test(str)`**: * Pros: Simple, straightforward implementation, and fast execution time since it only checks if there's a match. * Cons: May not be suitable for finding all matches or capturing groups. 2. **`re.exec(str)`**: * Pros: Can capture groups and find all matches in the string, but may have slower execution times due to additional operations. * Cons: Returns null if no match is found, which can lead to unnecessary checks. 3. **`str.match(re)`**: * Pros: Simple and intuitive implementation, similar to `re.test()`, with the added benefit of being able to use it on strings without creating a regex object first. * Cons: May have slightly slower execution times compared to `re.exec()` due to additional overhead. **Library Usage** In this benchmark, no libraries are explicitly used. However, some browser-specific features might be utilized under the hood (e.g., JavaScript engine optimizations), which could affect performance. **Special JavaScript Features or Syntax** There are no special features or syntax mentioned in this benchmark that would impact its behavior. The tests focus on comparing the performance of basic regex searching methods. **Alternatives** If you're interested in exploring alternative approaches, consider the following options: 1. **`String.prototype.replace()`**: While not directly applicable to finding matches, `replace()` can be used with a callback function to find and replace occurrences. 2. **`Array.prototype.find()` or `Array.prototype.findIndex()`**: These methods search for an element in an array using a callback function, which could be adapted for searching regex patterns. For a more comprehensive understanding of the performance differences between these approaches, I recommend checking out resources like JavaScript benchmarking platforms (e.g., jsperf) or exploring existing benchmarks that compare the execution times of different regex searching methods.
Related benchmarks:
RegEx.exec vs String.match
RegEx.exec vs regex.test
RegEx.exec vs String.match (inline)
Regex Exec vs String Split
RegExp.exec vs String.match vs RegExp.test vs RegExp.match
Comments
Confirm delete:
Do you really want to delete benchmark?