Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
endsWith vs Regex-literal
(version: 0)
Comparing performance of:
Regex vs endsWith
Created:
2 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var str = "this is a test"
Tests:
Regex
str.match(/^..._(?:BASE|PRO)$/)
endsWith
str.endsWith("_BASE") || str.endsWith("_PRO")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
endsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
endsWith
533.8M/s
Regex
26.4M/s
View exact numbers
Test name
Executions per second
✓
endsWith
533,838,880 Ops/sec
Regex
26,366,710 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its options. **Benchmark Overview** The benchmark compares two approaches for checking if a string ends with a specific suffix: using regular expressions (`Regex`) versus using the `endsWith` method. **Options Compared** There are two options being compared: 1. **Regex (Regular Expression)**: * Uses a pattern to match the string from the beginning (`^`) until a certain character or set of characters is found. * In this case, the pattern is `/^..._(?:BASE|PRO)$/`. 2. **endsWith**: * A built-in JavaScript method that checks if a string ends with a specified suffix. **Pros and Cons** Here are some pros and cons for each approach: **Regex** Pros: * Highly flexible and powerful pattern matching. * Can match multiple patterns at once. * Fast performance. Cons: * Steeper learning curve due to the complexity of regular expressions. * May be slower than `endsWith` in some cases, depending on the browser implementation. **endsWith** Pros: * Simple and easy to understand syntax. * Built-in method means it's implemented efficiently by most browsers. * Fast performance. Cons: * Limited flexibility compared to Regex (e.g., no support for complex patterns). * May not work in older browsers or environments that don't support `endsWith`. **Library Used** In this benchmark, the `RegExp` object is used to create a regular expression pattern. The `RegExp` object provides a way to define and search for patterns in strings. **Special JavaScript Feature/Syntax** There are no special JavaScript features or syntax being tested in this benchmark. Both options are straightforward and easy to understand. **Other Alternatives** If you needed to test other approaches, some alternatives could be: * Using a library like `regex-perf` (e.g., for more complex patterns) or `string-performance` (e.g., for string manipulation). * Testing alternative suffix checking methods, such as `substr()` or `slice()`. * Comparing the performance of different browsers' implementations of `endsWith`. Keep in mind that the benchmark's focus is on comparing two specific approaches, so these alternatives are not directly relevant to this particular benchmark.
Related benchmarks
endsWith vs Regex
endsWith vs Regex - case insensitive
endsWith vs Regex test
Comments
Confirm delete:
Do you really want to delete benchmark?