Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test substring vs slice vs substr
(version: 0)
Comparing performance of:
substr vs substring vs slice
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var example = '2019-10-22T15:41:42.739Z'
Tests:
substr
var result = example.substr(-1)
substring
var result = example.substring(0, example.length - 1)
slice
var result = example.slice(0, -1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
substr
substring
slice
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and analyze what's being tested. **What is being tested?** The provided benchmark tests three different approaches to extract the last character from a string: 1. `substr(-1)`: This uses the `-1` index to extract the last character of the string. 2. `substring(0, example.length - 1)`: This uses the `length` property and the `-1` index to extract the last character of the string. 3. `slice(0, -1)`: This uses the `slice()` method with two arguments: `0` (the start index) and `-1` (the end index). **Options compared** The benchmark compares these three approaches: * **Pros and Cons**: + `substr(-1)` is a simple and efficient way to extract the last character, but it's specific to the `substr()` method. + `substring(0, example.length - 1)` provides more control over the extraction process but requires additional calculations. It's also a bit slower due to the extra operations. + `slice(0, -1)` is a more modern and flexible way to extract parts of strings, as it uses an array-like approach with two arguments. However, it might be less efficient than the other two options for this specific use case. **Libraries used** None are explicitly mentioned in the benchmark definition or test cases. **Special JS features/syntax** No special JavaScript features or syntax are being tested in these benchmarks. **Other alternatives** If you wanted to test alternative approaches, here are some possibilities: * Using regular expressions (`/[^a-zA-Z0-9]+$/` or `/.+$/`) * Using string methods like `charCodeAt()` and `toString()` * Using a custom implementation with a loop * Using a more modern approach like using `slice()` or `substr()` with a callback function However, these alternatives are not tested in the provided benchmark. Overall, this benchmark provides a simple and straightforward test of three different approaches to extract the last character from a string.
Related benchmarks:
slice vs substring remove last char
slice vs substr vs substring (only start index)
slice vs substring with specified start and end
Performance Test: substring vs substr vs slice vs split for date
Comments
Confirm delete:
Do you really want to delete benchmark?