Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substr vs substring with end
(version: 0)
Compares slice, substr and substring to each other when there is only a start index
Comparing performance of:
slice vs substr vs substring
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var example = 'there is no spoon'
Tests:
slice
var result = example.slice(0,-1)
substr
var result = example.substr(10)
substring
var result = example.substring(0,example.length - 1)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
slice
substr
substring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
260353952.0 Ops/sec
substr
245863424.0 Ops/sec
substring
292488192.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark tests the performance of three different methods in JavaScript for extracting substrings from a string: `slice()`, `substr()`, and `substring()`. Let's break down each method and their options: **1. `slice()`:** This method extracts a portion of a string defined by two indices: * **Start Index (Required):** Where the substring should begin. * **End Index (Optional):** The index where the substring ends (exclusive). If omitted, it includes all characters from the start index to the end of the string. In this benchmark, `slice(0,-1)` extracts a substring starting at index 0 and ending at (but not including) the last character of the string `"there is no spoon"`. **2. `substr()`:** This method also extracts a portion of a string: * **Start Index (Required):** Where the substring should begin. * **Length (Required):** The number of characters to extract from the start index. In this benchmark, `substr(10)` extracts a substring starting at index 10 and taking `length` characters after that. This example doesn't specify the length, so it will include all characters after the 10th position in the string. **3. `substring()`:** * **Start Index (Required):** Where the substring should begin. * **End Index (Optional):** The index where the substring ends (exclusive). If omitted, it includes all characters from the start index to the end of the string. In this benchmark, `substring(0, example.length - 1)` extracts a substring starting at index 0 and ending at (but not including) the last character of the string. This is functionally similar to using `slice(0,-1)`. **Pros/Cons and Considerations:** * **`slice()`:** Often considered the most versatile due to its ability to handle both start and end indices, providing precise control over extracted portions. * **`substr()`:** Simpler for extracting a fixed length from a specific starting point. Less flexible than `slice()`. * **`substring()`:** Similar in functionality to `slice()` but can sometimes have slight performance variations. Less common due to the presence of `slice()`, which offers more flexibility. **Alternatives:** While these methods are standard in JavaScript, there are often alternative ways to achieve similar results, depending on your specific needs: * **Regular Expressions (`RegExp`):** Powerful for complex pattern matching and substring extraction, but can be less efficient for simple cases. * **String Methods (`indexOf`, `lastIndexOf`):** Can be combined to extract substrings by finding specific characters or patterns. Let me know if you have any other questions about this benchmark or JavaScript string manipulation techniques!
Related benchmarks:
slice vs substr vs substring (with end index) @fran
slice vs substring (with no end index)
slice vs substring (with end index)
slice vs substr vs substrings
Comments
Confirm delete:
Do you really want to delete benchmark?