Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
endsWith vs match for multiple strings
(version: 0)
Comparing performance of:
endsWith vs regex
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var matches = ['alpha', 'beta', 'charlie', 'delta']; var regex = new RegExp(`(${matches.join('|')})$`); var str = 'abcdefghijklmnopqrstuvwxyz';
Tests:
endsWith
matches.some(match => str.endsWith(match))
regex
regex.test(str)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
endsWith
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and what are the pros and cons of each approach. **Benchmark Overview** The benchmark measures the performance difference between using `String.prototype.endsWith()` method versus regular expression (`RegExp`) to check if a string ends with one or more specific substrings. The test case uses an array of strings `['alpha', 'beta', 'charlie', 'delta']` and concatenates them into a regex pattern. **Script Preparation Code** ```javascript var matches = ['alpha', 'beta', 'charlie', 'delta']; var regex = new RegExp(`(${matches.join('|')})$`); var str = 'abcdefghijklmnopqrstuvwxyz'; ``` This code creates: 1. An array of strings `matches` 2. A regular expression (`regex`) that matches any string in `matches` at the end of a target string 3. The test string `str` **Html Preparation Code** The benchmark doesn't require any HTML preparation code, which means it only tests the JavaScript execution and doesn't involve rendering or layout calculations. **Test Cases** There are two individual test cases: 1. **endsWith**: Tests using the `String.prototype.endsWith()` method to check if `str` ends with one of the strings in `matches`. 2. **regex**: Tests using a regular expression to match any string in `matches` at the end of `str`. **Pros and Cons** Here's a brief analysis of each approach: 1. **endsWith** * Pros: + Simple, straightforward implementation + Fast execution, as it only needs to check if the last character matches one of the expected values. * Cons: + May not be as efficient for large numbers of matching strings, as it creates multiple string concatenations 2. **regex** * Pros: + Flexible and can match any number of strings in `matches` at the end of `str` * Cons: + Requires a regex engine, which may introduce overhead + May not be as fast for large numbers of matching strings due to the complexity of regular expressions **Library (regex)** The regular expression library is built into most JavaScript engines, including modern browsers like Chrome. The `RegExp` constructor creates a new regex object that can be used for pattern matching. **Special JS Feature (none)** This benchmark doesn't use any special JavaScript features or syntax beyond the standard language. **Other Alternatives** If you're interested in exploring other approaches: 1. **Using a library like Regexp-approx**: This library provides an approximation of regular expression performance, which can be useful for testing purposes. 2. **Implementing a custom regex engine**: If you're familiar with compiler design or want to optimize the regex engine specifically for this use case, you could explore implementing your own regex engine. Keep in mind that these alternatives are not necessary for running the provided benchmark and may add additional complexity to your project.
Related benchmarks:
Regex First Name split vs match - no console
Regex vs split/join checking alphanumeric big number
Regex vs split/join 23313
endsWith vs Regex-literal
Comments
Confirm delete:
Do you really want to delete benchmark?