Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substr vs substring (with end index) @fran
(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:
5 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, 10)
substr
var result = example.substr(0, 10)
substring
var result = example.substring(0, 10)
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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
164953008.0 Ops/sec
substr
164829728.0 Ops/sec
substring
152793520.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! The provided JSON represents a benchmark test created on MeasureThat.net, which compares the performance of three string manipulation methods in JavaScript: `slice()`, `substr()`, and `substring()`. **What is tested?** These three methods are compared when used with only a start index. In other words, they are not used to extract a substring from another string or to get a specific part of an array. Instead, each method is called with the same input string (`"there is no spoon"`), but with different starting indices. **Options compared:** 1. `slice()` 2. `substr()` 3. `substring()` Each option has its own strengths and weaknesses: * **`slice()`**: This method returns a new string that includes all the characters from the original string, starting at the specified index (exclusive). It's generally faster than the other two methods because it doesn't need to check if the start index is within bounds. * **`substr()`**: This method also returns a new string with the specified length, but it stops when it reaches the end of the string. In this test case, it's slower than `slice()` because it needs to check if the requested length is less than or equal to the remaining characters in the string. * **`substring()`**: This method returns a new string that includes all the characters from the original string, starting at the specified index (exclusive). It's similar to `slice()`, but its performance might vary depending on the browser and JavaScript engine used. **Pros and cons:** * Pros: * `slice()`: Fastest because it doesn't need to check bounds. * `substring()` and `substr()`: May be slower due to bounds checking or length limitations. * Cons: * Performance differences may vary across browsers and JavaScript engines. * Each method creates a new string, which can lead to increased memory usage. **Library and its purpose:** None of the methods use any external libraries in this test case. They are built-in JavaScript functions that operate on strings. **Special JS feature or syntax:** This benchmark doesn't use any special features or syntax beyond standard JavaScript methods. However, it's worth noting that some browsers might have variations or extensions of these methods (e.g., `String.prototype.slice()` is not available in older browsers). **Other alternatives:** If you were to rewrite this benchmark, you could consider other string manipulation methods, such as: * Using regular expressions (`RegExp`) to extract parts of a string. * Utilizing array methods (`Array.prototype.slice()`, `Array.prototype.subarray()`) instead of strings' native methods. However, for simple string manipulation tasks like this one, the built-in JavaScript methods are usually sufficient and efficient enough.
Related benchmarks:
slice vs substring
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?