function compareEndsWith(val, comparator) {
return val.endsWith(comparator)
}
function compareEndsWith2(val, comparator) {
return val.endsWith(comparator, comparator.length)
}
function compareSliceMethod(val, comparator) {
const thingToCompare = val.slice(-comparator.length)
return thingToCompare === comparator
}
const value = Math.random().toString().padEnd(20,'0');
const ending = "asdf"
compareEndsWith(value, ending)
const value = Math.random().toString().padEnd(20,'0');
const ending = "asdf"
compareSliceMethod(value, ending)
const value = Math.random().toString().padEnd(20,'0');
const ending = "asdf"
compareEndsWith2(value, ending)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
endWith | |
slice | |
endsWith2 |
Test name | Executions per second |
---|---|
endWith | 4070914.8 Ops/sec |
slice | 4664534.0 Ops/sec |
endsWith2 | 4130641.2 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
The provided benchmark tests three different approaches for determining whether a string ends with a specific substring:
compareEndsWith
: This function uses the endsWith
method, which checks if the end of a string matches a specified value.compareEndsWith2
: This function also uses the endsWith
method, but it takes an additional argument specifying the length of the substring to match.compareSliceMethod
: This function slices the last comparator.length
characters from the input string and then compares them directly with the comparator.Options being compared
The three approaches are being tested against each other to determine which one is the fastest.
Pros and Cons:
compareEndsWith
: Pros: Simple and intuitive. Cons: May be slower if the substring is not found at the end of the string.compareEndsWith2
: Pros: Reduces overhead by passing the length of the comparator as an argument. Cons: May require more memory allocation due to the additional parameter.compareSliceMethod
: Pros: Can be faster for shorter substrings, as it avoids the overhead of endsWith
. Cons: Requires slicing the string, which can be slower for large strings.Library usage
None of the test cases explicitly use any external libraries. However, it's worth noting that the padEnd
method used in the benchmark preparation code is a part of the ECMAScript standard and is supported by most modern browsers.
Special JavaScript feature or syntax
The only notable feature used in this benchmark is the use of Math.random().toString().padEnd(20,'0')
, which generates a random string with 20 leading zeros. This is done to ensure that the comparison substrings ("asdf"
and the sliced strings) have different lengths, making it more representative of real-world scenarios.
Other alternatives
If you wanted to test alternative approaches for determining whether a string ends with a specific substring, some options could include:
new RegExp('.*' + ending)
).lodash
or string-polyfill
, which provides optimized implementations for various string manipulation methods.Keep in mind that these alternatives might have different performance characteristics and may not be as representative of real-world scenarios.