Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
substring vs substr vs slice
(version: 0)
Comparing performance of:
substring vs substr vs slice
Created:
6 years ago
by:
Registered User
Jump to the latest result
Tests:
substring
"0123456789".substring(9);
substr
"0123456789".substr(9);
slice
"0123456789".slice(-1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
substring
substr
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of three different ways to extract the last character from a string in JavaScript: * **`substring(9)`:** This method takes two arguments, a starting index and an ending index (exclusive). In this case, it starts at index 9 and goes until the end of the string. * **`substr(9)`:** Similar to `substring`, but it takes only two arguments: a starting index and a length. It extracts characters from the starting index for the specified length. This is more concise for extracting a single character. * **`slice(-1)`:** This method uses negative indexing, where -1 refers to the last element of the array. So `slice(-1)` will extract the last character of the string. **Pros and Cons:** * **`substring(9)`:** * **Pro:** Flexible for extracting substrings of varying lengths. * **Con:** Can be less efficient for simple extraction like this one because it iterates through the entire string from the starting index to the end. * **`substr(9)`:** * **Pro:** More concise and likely more efficient than `substring` when extracting a single character. * **Con:** Requires knowing the length of the substring you want to extract, which isn't always known beforehand. * **`slice(-1)`:** * **Pro:** The most concise and often the fastest method for extracting a single character from the end of a string because it directly accesses the last element. * **Con:** Only useful for extracting the last element; not suitable for extracting substrings of varying lengths. **Alternatives:** While these methods are common, there are alternatives depending on your specific needs: * **String's `charAt(index)`:** This method returns a single character at a given index, which can be used to extract the last character using `charAt(string.length - 1)`. * **Regular Expressions:** You could use a regular expression like `/(.*)$/` to capture the last character of a string. Let me know if you have any more questions!
Related benchmarks:
slice vs substring remove last char
Performance Test: substring vs substr vs slice constant length
slice vs substr vs substring (only start index)
slice vs substr vs substring 122459
Comments
Confirm delete:
Do you really want to delete benchmark?