var string = 'x';
var stringToCheck = 'alksjdflaksjdlkajtlkadjsta;ksdglkasdjfadsfaf';
var result = null;
result = stringToCheck.endsWith(string);
result = stringToCheck[ stringToCheck.length - 1 ] === string;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
.endsWith | |
.includes |
Test name | Executions per second |
---|---|
.endsWith | 260660864.0 Ops/sec |
.includes | 267942080.0 Ops/sec |
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:
.endsWith()
: This method checks if the last character of the stringToCheck
is equal to the given string
.stringToCheck[ stringToCheck.length - 1 ] === string
: This approach directly compares the last character of the string with the given string.Pros and Cons:
.endsWith()
:stringToCheck[ stringToCheck.length - 1 ] === 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:
/[^x]*$/
): This would match any character followed by the given string (excluding the last one) and captures the last character.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.