Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript string.endsWith vs string.slice and tripple equal
(version: 0)
Comparing performance of:
endWith vs slice
Created:
5 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(-comparator.length) return thingToCompare === comparator }
Tests:
endWith
const value = "douche nation" const ending = "tion" compareEndsWith(value, ending)
slice
const value = "douche nation" const ending = "tion" 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:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
endWith
63260504.0 Ops/sec
slice
92820192.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll dive into explaining the benchmark. **What is tested:** The provided benchmark tests two approaches for checking if a string ends with a certain substring: 1. `endsWith`: A method that checks if a string ends with another string using the `String.prototype.endsWith()` method. 2. `slice` and `triple equal (`): An approach that uses the `slice()` method to extract the last few characters of the string, compares them using triple equals (`===`) with the target substring. **Options compared:** The benchmark compares the performance of: 1. `endsWith` 2. `slice` and `triple equal` **Pros and Cons:** * **endsWith**: + Pros: Efficient, reliable, and widely supported by browsers. + Cons: May be slower for very long strings due to regular expression overhead. * **Slice and triple equal**: + Pros: Simple, lightweight, and efficient for short substrings. + Cons: May lead to issues with edge cases (e.g., null or undefined values), and is not as widely supported by browsers. **Library and purpose:** In this benchmark, the `slice()` method is used, which is a built-in JavaScript method. The `slice()` method takes two arguments: the start index and the end index. In this case, the end index is calculated using the length of the target substring (`-comparator.length`). **Special JS feature or syntax:** There are no special features or syntax used in this benchmark. **Other alternatives:** Alternative approaches for checking if a string ends with another string include: * Using a regular expression (`/endsWith/g`): This method is more efficient than `endsWith` but may be slower for very long strings. * Using the `lastIndexOf()` method: This method returns the index of the last occurrence of the target substring, which can be used to determine if the string ends with it. For example: ```javascript function compareLastIndexOf(val, comparator) { return val.lastIndexOf(comparator) === val.length - comparator.length; } ``` However, these alternatives are not tested in this benchmark.
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?