const string = "banana_pie";
string.at(-1);
const string = "banana_pie";
string[string.length-1];
const string = "banana_pie";
string.slice(-1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
at(-1) | |
length-1 | |
slice(-1) |
Test name | Executions per second |
---|---|
at(-1) | 16872882.0 Ops/sec |
length-1 | 29978592.0 Ops/sec |
slice(-1) | 26858438.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmarked Methods
The three methods being benchmarked are used to extract the last character from a string:
string.at(-1)
: This method uses the at()
function, which returns the nth property of an object or array, or undefined if the index is out of range. In this case, it's used with a negative index -1
to get the last character.string[string.length-1]
: This method uses string indexing with a calculated value string.length - 1
, which is equivalent to getting the last character using at(-1)
.string.slice(-1)
: This method uses the slice()
function, which returns a new string containing a subset of characters from an original string. When used with -1
as the offset, it extracts the last character.Comparison
The benchmark compares the performance of these three methods on different JavaScript engines and browsers:
at(-1)
vs string[string.length-1]
: Both methods are equivalent, but at(-1)
might be slightly faster due to its more concise syntax.at(-1)
vs slice(-1)
: The winner depends on the specific browser and engine. However, in general, at(-1)
is expected to be faster since it's a single property access, whereas slice()
creates a new string object.Pros and Cons
Here are some pros and cons of each method:
string.at(-1)
: Pros:string[string.length-1]
: Pros: Widely supported across modern browsers, equivalent to at(-1)
in terms of performance. Cons: Less concise syntax compared to at(-1)
.string.slice(-1)
: Pros:at()
. Cons:at(-1)
.Library
None of the benchmarked methods rely on external libraries. However, if you're interested in understanding more about the properties used:
string.at()
: Introduced in ECMAScript 2019 (ES11), this property allows accessing array-like objects using a numeric index.string.slice()
: This function has been part of JavaScript since its inception and is widely supported across all browsers.Special JS Features/Syntax
The benchmark uses the following special features:
at(-1)
: A new property introduced in ECMAScript 2019 (ES11), allowing array-like object access using a negative index.slice(-1)
: Uses the slice()
function, which is widely supported but might not be as concise or efficient as at(-1)
.Alternatives
If you're looking for alternative ways to extract the last character from a string, here are some options:
substr()
method: string.substr(string.length - 1)
.\w
(match any word character, including underscores) followed by $
(assert end of string).Keep in mind that these alternatives might not be as efficient or concise as the original methods.