Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
willBeReplaced
(version: 0)
Comparing performance of:
regex vs regular
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
regex
/(...\s|--\s|['"])$/.test('Hello hello hello John 3:16 this is not a drill..') || /^(\* |- |1\. |1\) )$/.test('1) ')
regular
['... ', '-- ', "'", '"'].some(str => 'Hello hello hello John 3:16 this is not a drill..'.endsWith(str)) || ['* ', '- ', '1. ', '1) '].some(str => '1) ' === str)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regex
regular
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex
19817584.0 Ops/sec
regular
49099780.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the benchmark. **Benchmark Overview** MeasureThat.net is a website where users can create and run JavaScript microbenchmarks. The provided benchmark has two test cases: `regex` and `regular`. **Test Case 1: regex** The first test case checks if a string matches a regular expression pattern using the `RegExp.test()` method. ```json "Benchmark Definition": "/(...\\s|--\\s|['\'])$/.test('Hello hello hello John 3:16 this is not a drill..') || /^(\\* |- |1\\. |1\\) )$/.test('1) ')" ``` This pattern checks for: - `...` followed by any whitespace (`\\s`) - `--` followed by any whitespace - Single quotes (`'`) or double quotes (`"`) - Any of the above (the `||` operator) The test case is checking if the input string matches this pattern. The expected output should be `true`. **Test Case 2: regular** The second test case uses a different approach to check for whitespace and numeric characters. ```json "Benchmark Definition": "['... ', '-- ', \"'\", '\"'].some(str => 'Hello hello hello John 3:16 this is not a drill..'.endsWith(str)) || ['* ', '- ', '1. ', '1) '].some(str => '1) ' === str)" ``` This test case uses the `String.endsWith()` method to check if the input string ends with each of the specified whitespace and numeric characters. The first part (`['... ', '-- ', \"'\", '\"'].some(...)`) checks for each of these whitespace characters, while the second part (`['* ', '- ', '1. ', '1) '].some(...)`) checks for each of these numeric characters. **Library: RegExp** The `RegExp` library is used in both test cases to implement regular expressions. The `test()` method is a part of the `RegExp` object, which is used to perform a match operation on a string. **Special JS Features/Syntax** Neither of the test cases uses any special JavaScript features or syntax that requires additional explanation. **Other Alternatives** There are other approaches to implementing regular expressions in JavaScript: 1. **String.prototype.match()**: This method returns an array containing all matches of the pattern in the string. 2. **String.prototype.includes()`: This method checks if a substring is present in the string, but it's not suitable for this use case. **Pros and Cons** Here are some pros and cons of each approach: 1. `RegExp.test()`: * Pros: Fast and efficient, allows for more complex patterns. * Cons: Can be confusing to read and write, may not work well with modern JavaScript features like string interpolation. 2. `String.prototype.match()`: * Pros: Easy to read and write, works well with modern JavaScript features. * Cons: Slower than `RegExp.test()` for simple patterns, can return an array even if only one match is found. 3. `String.prototype.includes()`: Not recommended for this use case. **Browser-Specific Considerations** The provided benchmark results show that the browser being tested (Chrome 123) has significantly different execution times for each test case. This may be due to differences in how the browser handles regular expressions or string matching. In general, it's essential to consider browser-specific issues when writing benchmarks, as different browsers can have varying performance characteristics and limitations.
Related benchmarks:
Unnecessary non-capturing groups
Class vs ID
_last vs regex
lodash _.includes vs regex
Array from vs string split with large strings
Comments
Confirm delete:
Do you really want to delete benchmark?