Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
endsWith vs regex match
(version: 0)
Comparing performance of:
endsWith vs regexMatch
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testString = 'subdomain.awesome.com'; var result = null;
Tests:
endsWith
testString.endsWith('awesome.com') == true
regexMatch
testString.match(/(subdomain|another).awesome.com/g).length > 0
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
endsWith
regexMatch
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
endsWith
20379908.0 Ops/sec
regexMatch
12019155.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to check if a string ends with a specific substring: 1. `endsWith`: Using the `String.endsWith()` method, which checks if the original string ends with the specified value. 2. `regexMatch`: Using a regular expression (`/.../g`) to search for the specified substring anywhere in the string. **Options Compared** The benchmark is testing two options: * `endsWith`: Uses a dedicated method (`String.endsWith()`) to check if the string ends with a specific value. * `regexMatch`: Uses a regular expression to search for the specified substring anywhere in the string. **Pros and Cons of Each Approach** 1. `endsWith`: * Pros: + Efficient, as it only checks the last character(s) of the string. + Can be faster than regex matching. * Cons: + May not work correctly for strings with non-ASCII characters or Unicode normalization issues. 2. `regexMatch`: * Pros: + More flexible and can match substrings anywhere in the string, including multiple occurrences. + Works correctly with non-ASCII characters and Unicode normalization issues. * Cons: + Can be slower than `endsWith`, especially for large strings or repeated searches. + Requires additional overhead to parse and execute the regular expression. **Library Used** There is no explicit library used in this benchmark, but we can assume that it's using the built-in JavaScript `String` object and its methods. **Special JS Features or Syntax** This benchmark doesn't seem to use any special JavaScript features or syntax. It's a straightforward comparison of two approaches to check if a string ends with a specific substring. **Other Alternatives** If you're looking for alternative approaches, here are a few: * `indexOf()` + `slice()`: This approach involves finding the index of the specified substring in the original string and then checking if it's not -1. While it might work correctly, it's less efficient than using `endsWith` or regex matching. * `RegExp.test()`: Similar to `regexMatch`, but uses a simpler syntax and can be faster for single occurrences. Keep in mind that these alternatives are not as efficient or flexible as the original approaches being compared in this benchmark.
Related benchmarks:
substring vs regex
Test regex2
regecxt
RegEx.test vs. String.includes checking if https:// or http:// exists
Comments
Confirm delete:
Do you really want to delete benchmark?