Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript string.endsWith vs string.slice and tripple equal (one char)
(version: 0)
Comparing performance of:
endWith vs slice
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const value = "douche nation" const ending = "tion" 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:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
endWith
34449516.0 Ops/sec
slice
33287678.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the benchmark and its options. **Benchmark Overview** The benchmark measures the performance of two different approaches for checking if a string ends with a certain character: `endsWith` and `slice()` + `===`. The test case uses only built-in JavaScript functions, without any external libraries or special features. **Options Compared** There are two main options compared in this benchmark: 1. **`endsWith`**: This function takes two arguments: the string to check, and the character to search for at the end of the string. It returns a boolean value indicating whether the character is found at the end of the string. 2. **`slice()` + `===`**: This approach uses the `slice()` method to extract the last character of the string, and then compares it with the expected character using the `===` operator. **Pros and Cons** **`endsWith`**: Pros: * More concise and readable code * Less likely to cause performance issues due to the simplicity of the operation Cons: * May be slower than the alternative approach on some browsers or JavaScript engines, as it involves a function call overhead **`slice()` + `===`**: Pros: * Can be faster than `endsWith` on some browsers or JavaScript engines, especially for longer strings * Allows for better control over the performance of the operation Cons: * Less readable code due to the use of an additional method and operator * May cause performance issues if not implemented correctly (e.g., using a large slice size) **Library** In this benchmark, there is no external library used. The `slice()` function is part of the built-in JavaScript API. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in this benchmark. It only relies on standard JavaScript functions and operators. **Other Considerations** * **Browser Support**: Both approaches should work in most modern browsers, but it's essential to ensure that they are compatible with the target audience. * **String Length**: The performance difference between `endsWith` and `slice()` + `===` may be more noticeable for longer strings. This benchmark measures both cases to provide a comprehensive understanding of the trade-offs. **Alternative Approaches** Other alternatives could include: 1. Using regular expressions (`RegExp.test()`) to check if the string ends with a certain pattern. 2. Implementing a custom function using bitwise operations (e.g., `str.charCodeAt(str.length - 1) === charCodeAt`) for a potential performance boost on some browsers or JavaScript engines. However, these alternatives are less common and may not be as widely supported or efficient as `endsWith` and `slice()` + `===`.
Related benchmarks:
Array.prototype.slice vs spread operator (forked 2)
Array.prototype.slice vs spread operator but using params
Array.prototype.slice + sort vs spread operator
Array.prototype.slice vs spread operator vs concat 2
Array.prototype.slice vs spread operator but corrent
Comments
Confirm delete:
Do you really want to delete benchmark?