Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
endsWith() vs regex
(version: 0)
Comparing performance of:
endsWith vs regex
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
endsWith
'Hello World'.endsWith('rld')
regex
/rld$/.test('Hello World')
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:
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/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
endsWith
178022592.0 Ops/sec
regex
65938464.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Overview** The benchmark compares two approaches to check if a string ends with a specific substring: the `endsWith()` method and regular expressions (regex). **Options compared** There are two options being compared: 1. **endsWith() method**: This method checks if a string ends with a specified value. 2. **Regular Expressions (regex)**: Specifically, the `/rld$/` pattern is used to match strings that end with the substring "rld". **Pros and Cons of each approach** * `endsWith()` method: + Pros: - More readable and concise syntax. - Can be faster since it's a native JavaScript method. + Cons: - May not work as expected for non-English characters, as it relies on Unicode normalization. - May have performance issues if the string is very long or if the substring is not found. * Regex (`/rld$/` pattern): + Pros: - Can handle any character set and can be more flexible than `endsWith()`. - Performance is generally better for longer strings or when searching for a specific pattern. + Cons: - More complex syntax, which may lead to readability issues. - May require additional setup (e.g., Unicode normalization) if used with non-English characters. **Library and Purpose** In this benchmark, no library is explicitly mentioned. However, the use of regex implies that JavaScript's built-in `RegExp` object or a similar implementation is being utilized. **Special JS feature or syntax** There are no special JS features or syntaxes mentioned in this benchmark. The focus is on comparing two common string manipulation approaches. **Other alternatives** Other alternatives for checking if a string ends with a specific substring include: * Using the `String.prototype.slice()` method with a negative index to extract the ending part of the string. * Utilizing a library like Lodash's `endsWith()` function, which provides a more robust implementation of this functionality. * Implementing custom logic using bitwise operations or other algorithms optimized for performance. **Benchmark preparation code** Since no specific preparation code is provided in the benchmark definition JSON, it's likely that the test cases are designed to be run with minimal setup. In practice, you would typically include code to: * Define the string and substring being tested. * Prepare any necessary data structures or variables. * Set up the testing environment (e.g., executing a function that runs the tests). **Individual test cases** The provided benchmark definition includes two individual test cases: 1. `endsWith`: Tests if "Hello World" ends with the substring "rld". 2. `regex`: Tests if "Hello World" matches the regex pattern `/rld$/.
Related benchmarks:
endsWith vs Regex extension
endsWith vs Regex - case insensitive
endsWith vs Regex correct
endsWith vs Regex-literal
Comments
Confirm delete:
Do you really want to delete benchmark?