Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string.at(-1) vs string[string.length-1] vs string.slice(-1)
(version: 0)
Some methods to get the last character of a string
Comparing performance of:
at(-1) vs length-1 vs slice(-1)
Created:
one year ago
by:
Guest
Go to the latest result
Tests:
at(-1)
const string = "banana_pie"; string.at(-1);
length-1
const string = "banana_pie"; string[string.length-1];
slice(-1)
const string = "banana_pie"; string.slice(-1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
at(-1)
length-1
slice(-1)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 127 on Android
View result in a separate tab
Embed
Embed Benchmark Result
length-1
30.0M/s
slice(-1)
26.9M/s
at(-1)
16.9M/s
View exact numbers
Test name
Executions per second
✓
length-1
29,978,592 Ops/sec
slice(-1)
26,858,438 Ops/sec
at(-1)
16,872,882 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. `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. 2. `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)`. 3. `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: + Concise syntax + Fast execution (due to optimized property access) Cons: Not supported in older browsers. * `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: + Can be used to extract multiple characters at once (not just the last one). + Supports older browsers that don't support `at()`. Cons: - Creates a new string object, which might have performance implications. - Less concise syntax compared to `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: * Using `substr()` method: `string.substr(string.length - 1)`. * Using regular expressions: `\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.
Related benchmarks
char index vs charAt() vs slice() for the last character
charAt() vs slice()
string.at(-1) vs string[string.length-1]
Comments
Confirm delete:
Do you really want to delete benchmark?