Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.endsWith vs last char
(version: 0)
Comparing performance of:
.endsWith vs .includes
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = 'x'; var stringToCheck = 'alksjdflaksjdlkajtlkadjsta;ksdglkasdjfadsfaf'; var result = null;
Tests:
.endsWith
result = stringToCheck.endsWith(string);
.includes
result = stringToCheck[ stringToCheck.length - 1 ] === string;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.endsWith
.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark test case. **What is being tested?** The benchmark test case compares two approaches to check if a string ends with a specific character or substring: `.endsWith()` and indexing into the string using `stringToCheck[ stringToCheck.length - 1 ] === string`. **Options compared:** 1. `.endsWith()`: This method checks if the last character of the `stringToCheck` is equal to the given `string`. 2. Indexing into the string using `stringToCheck[ stringToCheck.length - 1 ] === string`: This approach directly compares the last character of the string with the given string. **Pros and Cons:** * `.endsWith()`: + Pros: - More readable and easier to understand, as it clearly conveys the intention. - Less error-prone, as it avoids direct indexing into the string. + Cons: - May be slower due to method call overhead. * Indexing into the string using `stringToCheck[ stringToCheck.length - 1 ] === string`: + Pros: - Faster, as it avoids method call overhead and directly accesses the last character of the string. + Cons: - Less readable and more prone to errors, as it relies on indexing into a potentially large string. **Library usage:** There is no library explicitly mentioned in this benchmark. However, `.endsWith()` is a built-in JavaScript method provided by the ECMAScript standard. **Special JS feature or syntax:** This benchmark uses the ".endsWith" and ".includes" methods, which are part of the ECMAScript standard. **Other alternatives:** If you want to implement an alternative approach that avoids using built-in methods like `.endsWith()` and indexing into the string, you could consider: 1. Using a regular expression (`/[^x]*$/`): This would match any character followed by the given string (excluding the last one) and captures the last character. 2. Implementing a custom loop to iterate through the characters of the string from the end: This would involve manually accessing each character in reverse order and comparing it with the given string. Keep in mind that these alternatives may not be as readable or maintainable as using built-in methods like `.endsWith()` and indexing into the string, but they can provide a learning experience for understanding how string manipulation works in JavaScript.
Related benchmarks:
.endsWith vs includes
.endsWith vs last char check
.endsWith vs last c
String.includes vs. multiple String.endsWith
string.at(-1) vs string[string.length-1]
Comments
Confirm delete:
Do you really want to delete benchmark?