Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
endsWith slice vs Regex replace
(version: 0)
Comparing performance of:
Regex vs endsWith vs regex neg vs endsWith neg
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Regex
const str = "test/"; str.replace(/\/$/, '')
endsWith
const str = "test/"; if (str.endsWith('/')) str.slice(0, -1)
regex neg
const str = "test"; str.replace(/\/$/, '')
endsWith neg
const str = "test"; if (str.endsWith('/')) str.slice(0, -1)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Regex
endsWith
regex neg
endsWith neg
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (iPad; CPU OS 18_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.3
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
443450176.0 Ops/sec
endsWith
50494660.0 Ops/sec
regex neg
401532608.0 Ops/sec
endsWith neg
48346952.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 the performance of three approaches to check if a string ends with a certain substring: using a regular expression (`regex`), using the `endsWith` method, and using a combination of slicing (`slice`) and negation (`neg`). There are two variations for each approach: one that uses the original string (`original`) and another that uses an empty string as the suffix to check against (`neg`). **Options Compared** 1. **Regular Expression (Regex)**: * Using `str.replace(/\\/$/, '')` to remove the last character from the string, which is equivalent to checking if the string ends with a single slash. * The regular expression `/\\/$/` matches a literal backslash (`\`) followed by the end of the string (`$`). The `\` is escaped because it has a special meaning in regex. 2. **endsWith Method**: * Using `str.endsWith('/')` to check if the string ends with a single slash. 3. **Slicing and Negation (Slice)**: * Using `str.slice(0, -1)` to remove the last character from the string, which is equivalent to checking if the string ends with a single slash. **Pros and Cons of Each Approach** 1. **Regular Expression (Regex)**: * Pros: Can be used to match more complex patterns, often faster in modern JavaScript engines. * Cons: May be slower for simple checks like this, especially in older browsers or with large strings. 2. **endsWith Method**: * Pros: Simple and straightforward, well-supported by most browsers. * Cons: May not work as expected in certain edge cases (e.g., null or undefined strings). 3. **Slicing and Negation (Slice)**: * Pros: Fast and efficient, but may be slower than regex for more complex checks. * Cons: May not be supported by older browsers or with large strings. **Library Usage** None of the benchmark test cases use a library, so there is no additional overhead to consider. **Special JS Features or Syntax** The only special feature used in this benchmark is the `endsWith` method, which is a built-in method in modern JavaScript. However, it's worth noting that some older browsers may not support this method. **Alternative Approaches** If you need to check if a string ends with a specific substring, other approaches could include: 1. Using a loop to iterate through the characters of the string. 2. Using a regex with the `^` anchor, which matches the start of the string and ensures that only the specified suffix is matched. Keep in mind that these alternatives may be slower or less efficient than the methods tested in this benchmark.
Related benchmarks:
split vs exec vs replace vs slice
split index 0 vs regex replace
split index 0 vs regex replace v2
slice vs replace (same search)
Comments
Confirm delete:
Do you really want to delete benchmark?