Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test regex vs match vs includes
(version: 0)
Comparing performance of:
regex vs includes vs match
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var num = "1.899999999996"
Tests:
regex
/^\d+\.\d{3,}$/.test(num)
includes
num.includes('.')
match
num.match('.')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
regex
includes
match
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex
18377220.0 Ops/sec
includes
95900504.0 Ops/sec
match
5046630.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and individual test cases to understand what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of three different methods: `includes()`, `regex` using the `/test()` method, and `match()` with a regular expression. The goal is to determine which method is the fastest for this specific use case. **Script Preparation Code** The script preparation code defines a variable `num` containing a decimal number in string format: `"1.899999999996"`. **Individual Test Cases** There are three test cases: 1. **Regex** * `Benchmark Definition`: `/^\\d+\\.\\d{3,}$/.test(num)` * Purpose: This regular expression checks if the input string starts with a digit followed by a decimal point and exactly 3 more digits. 2. **Includes** * `Benchmark Definition`: `num.includes('.')` * Purpose: This method checks if the input string contains a specific character (in this case, the dot `'.'`). 3. **Match** * `Benchmark Definition`: `num.match('.')` * Purpose: This method searches for a specific pattern in the input string (in this case, the dot `'.'`) and returns an array of matches or `null` if no match is found. **Library Used** None. **Special JavaScript Features/Syntax** There are no special features or syntax used in these test cases. Now, let's discuss the pros and cons of each approach: 1. **Regex** * Pros: Regular expressions can be powerful and flexible for pattern matching. * Cons: + Can be slower than other methods due to the overhead of compiling and executing regular expressions. + May have a higher memory footprint due to the need to store the compiled regex pattern. 2. **Includes** * Pros: The `includes()` method is generally faster than regular expressions, as it uses a simple string search. * Cons: + May not be suitable for complex string searches or pattern matching. 3. **Match** * Pros: + Similar to the `includes()` method, but can also handle more complex patterns using regular expression syntax. * Cons: + Requires explicit compilation of the regex pattern, which may add overhead. **Alternative Approaches** Other alternatives for this benchmark could include: 1. Using a dedicated string search library or function (e.g., `String.prototype.indexOf()` or `String.prototype.startsWith()`). 2. Implementing a custom pattern matching algorithm using techniques like Boyer-Moore or Knuth-Morris-Pratt. 3. Comparing the performance of different JavaScript engines or versions. However, it's worth noting that the `includes()` method and regular expressions are both widely supported in modern JavaScript environments, making them suitable for this benchmark.
Related benchmarks:
RegExp.test() vs String.match()
Float string optimization: parseFloat() vs regex, full version
.test vs .match vs modulo(string)
Regex vs split/join checking alphanumeric big number
isNaN vs regex test for stringify number check
Comments
Confirm delete:
Do you really want to delete benchmark?