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:
9 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
llama3.1:latest
, generated one year ago):
Let's dive into the benchmark test case. **What is being tested?** The benchmark is comparing three different ways to extract a substring from a string in JavaScript: `slice`, `substr`, and `substring`. **Description of options** 1. **`slice`**: The `slice` method is used to extract a section of an array or a subset of characters from a string. In this test case, it's being used to extract the last character from the string `'there is no spoon'`. The syntax is `string.slice(startIndex)`, where `startIndex` is the position where you want to start extracting characters. 2. **`substr`**: The `substr` method (which is deprecated in modern JavaScript) is used to extract a subset of characters from a string, starting at a specified index and optionally ending at another index. In this test case, it's being used to extract the substring starting from index 10 (i.e., `'o no spoon'`) without specifying an end index. 3. **`substring`**: The `substring` method is similar to `substr`, but it requires two indices: one for the start position and another for the end position. In this test case, it's being used to extract the substring from index 0 (i.e., the beginning of the string) to index `example.length - 1`, effectively excluding the last character. **Pros and cons** Here are some pros and cons of each method: * **`slice`**: Pros: more modern and efficient; Cons: may be slower for large strings or edge cases. * **`substr` (deprecated)**: Pros: None; Cons: slower, less efficient, and should not be used in new code. * **`substring`**: Pros: simple to use and understand; Cons: may be slower for large strings or edge cases. **Other considerations** * The test case uses a string `'there is no spoon'`, which has a length of 16 characters. This might affect the performance comparison between the methods, as `slice` and `substring` need to calculate the end index. * The test case is running on Firefox 74, so the results may vary on other browsers or JavaScript engines. **Library** No external library is used in this benchmark. **Special JS feature or syntax** None are mentioned in this specific test case. However, note that `slice` and `substring` methods use a different indexing approach than `substr`, where indices start from 0 instead of the position immediately after the string's first character.
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?