Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript string.endsWith vs string.slice and tripple equal checking single character
(version: 1)
Comparing performance of:
endWith vs slice
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const value = "douche nation" const ending = "n" function compareEndsWith(val, comparator) { return val.endsWith(comparator) } function compareSliceMethod(val, comparator) { const thingToCompare = val.slice(-1) return thingToCompare === comparator }
Tests:
endWith
const value = "douche nation" const ending = "n" compareEndsWith(value, ending)
slice
const value = "douche nation" const ending = "n" compareSliceMethod(value, ending)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
endWith
slice
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/605.1.15 (KHTML, like Gecko) Version/18.4 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
endWith
79299184.0 Ops/sec
slice
512264416.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided compares two different methods for checking if a string ends with a specific character in JavaScript. The two methods being tested are `String.prototype.endsWith()` and a combination of `String.prototype.slice()` and strict equality (`===`) comparison. ### Methods Compared 1. **Using `endsWith` Method** - **Function**: `compareEndsWith(val, comparator)` - **Implementation**: This method directly checks if the string `val` ends with the substring `comparator`. - **Usage**: ```javascript function compareEndsWith(val, comparator) { return val.endsWith(comparator); } ``` 2. **Using `slice` and Strict Equality** - **Function**: `compareSliceMethod(val, comparator)` - **Implementation**: This method retrieves the last character of the string `val` using `slice(-1)` and then checks if it is equal to `comparator` using strict equality (`===`). - **Usage**: ```javascript function compareSliceMethod(val, comparator) { const thingToCompare = val.slice(-1); return thingToCompare === comparator; } ``` ### Performance Results The benchmark results indicate the number of executions per second for each method on a Mac OS X system using Safari 18: - **`endsWith` Method**: 79,299,184 executions per second - **`slice` Method**: 512,264,416 executions per second ### Pros and Cons **1. `endsWith` Method:** - **Pros**: - Readability: It’s more straightforward and conveys intent clearly that you are checking the end of a string. - Directly optimized for the purpose of checking if a string ends with a specific substring. - **Cons**: - Performance: In this benchmark, it is significantly slower than the `slice` method. **2. `slice` and Strict Equality Combination:** - **Pros**: - Performance: This approach is faster according to the benchmark results, making it more efficient in scenarios where performance is critical. - **Cons**: - Readability: This method is less concise and may be less immediately clear to someone reading the code without context. It also uses two operations instead of one. ### Other Considerations - **Browser Compatibility**: Both methods are widely supported across modern browsers, so compatibility issues are minimal. - **Use Case**: If performance is a significant concern and the code is executed in performance-critical sections, using `slice` may be the preferred approach. However, if readability and maintainability are prioritized, `endsWith` could be favorable. ### Alternatives - **Regular Expressions**: You could also use regex to check if a string ends with a particular character. For example: ```javascript const regex = new RegExp(`${comparator}$`); return regex.test(val); ``` - **Pros**: It's powerful and flexible for patterns. - **Cons**: It is generally slower than the direct methods and might be overkill for simple checks. - **Manual Character Comparison**: If you're checking only single-character endings, a simple comparison can be performed: ```javascript return val.charAt(val.length - 1) === comparator; ``` - This might be as efficient as `slice` while maintaining a clear intention. Overall, the benchmark highlights the trade-off between performance and readability in string manipulation methods in JavaScript, allowing developers to make informed decisions based on their specific use cases.
Related benchmarks:
string lookup
Compare direct v funx
Compare direct v funx2
test if/else and if/else-if 2
substring vs slice
.endsWith vs last char
Performance Test: substring vs substr vs slicey
slice vs substring vs [] vs charAt vs charCodeAt
Javascript string.endsWith vs string.slice and triple equal checking vs hardcoded index and triple equal, comparing a single character
Comments
Confirm delete:
Do you really want to delete benchmark?