Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string.at(-1) vs string[string.length-1]
(version: 0)
Couple of methods to get the last character of a string
Comparing performance of:
at(-1) vs length-1
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
at(-1)
const string = "banana_pie"; string.at(-1);
length-1
const string = "banana_pie"; string[string.length-1]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
at(-1)
length-1
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
at(-1)
119131024.0 Ops/sec
length-1
244947072.0 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. **Benchmark Overview** The benchmark is comparing two methods to get the last character of a string: `string.at(-1)` and `string[string.length-1]`. **Options Being Compared** There are two main options being compared: 1. **`string.at(-1)`**: This method uses the `at()` function introduced in ECMAScript 2019 (ES2020). It returns the nth element of an array, where n is the index passed as a string. In this case, it's used to get the last character of a string. 2. **`string[string.length-1]`**: This method uses string indexing and returns the character at the last index of the string. **Pros and Cons** Here are some pros and cons of each approach: * `string.at(-1)`: + Pros: More readable and intuitive, as it's similar to array indexing. + Cons: Requires ES2020 support, which might not be available in older browsers or environments. * `string[string.length-1]`: + Pros: Widely supported across older browsers and environments. + Cons: Less readable and more verbose, as it requires string length calculation. **Library/Feature Used** The benchmark uses the `at()` function introduced in ECMAScript 2019 (ES2020). This is a new JavaScript feature that allows indexing into arrays and objects using strings. It's used to provide a more modern and expressive way of accessing array elements, but it may not be supported in older browsers or environments. **Other Considerations** When testing these two methods, other considerations come into play: * **Performance**: Measuring the performance difference between `string.at(-1)` and `string[string.length-1]` can help identify which method is more efficient. * **Readability**: The choice of method can affect code readability. Some developers might prefer `string[at(-1)]` over `string[string.length-1]` due to its more intuitive syntax. **Alternative Approaches** Other alternative approaches for getting the last character of a string include: * Using the `substr()` or `slice()` methods, which return a subset of characters from the original string. * Using regular expressions (`RegExp.prototype.exec()`). * Using string concatenation or other workarounds (less common and less efficient). In summary, the benchmark is comparing two methods for getting the last character of a string: `string.at(-1)` and `string[string.length-1]`. The choice between these options depends on factors like performance, readability, and compatibility with older browsers or environments.
Related benchmarks:
charAt() vs slice()
slice vs substring from end
String.includes vs. multiple String.endsWith
string.at(-1) vs string[string.length-1] vs string.slice(-1)
Comments
Confirm delete:
Do you really want to delete benchmark?