Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
substr vs substring (with end index)
(version: 0)
Compares slice, substr and substring to each other when there is only a start index
Comparing performance of:
substr vs substring
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var example = 'there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon there is no spoon '
Tests:
substr
var result = example.substr(75, 10)
substring
var result = example.substring(75, 85)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
substr
substring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
substr
16327437.0 Ops/sec
substring
16472260.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 case on MeasureThat.net, which compares the performance of two string slicing methods: `substr` and `substring`. The test is designed to measure the execution time difference between these two approaches when only a start index is provided. **Options Compared:** 1. **substr**: This method returns a new string containing a subset of characters from the original string, starting at the specified index. 2. **substring**: This method also returns a new string containing a subset of characters from the original string, but it requires an optional end index to specify the length of the substring. **Pros and Cons:** 1. **substr**: * Pros: Simple and concise syntax. * Cons: It creates a new string object, which can lead to memory allocation overhead. 2. **substring**: * Pros: Provides more control over the substring's length. * Cons: Requires an additional index (end) for specifying the substring's length. In general, `substr` is faster because it only requires a single index operation, whereas `substring` needs to perform two operations: one to get the substring up to the end index and another to get the remaining characters after the end index. However, when you need more control over the substring's length, `substring` can be a better choice. **Library/Functionality:** None of the mentioned methods rely on any external libraries or functions. They are built-in JavaScript methods that work with string objects. **Special JS Feature/Syntax:** There is no special feature or syntax used in this benchmark test. It only utilizes standard JavaScript string slicing methods. **Other Alternatives:** If you're interested in exploring other string slicing methods, here are a few examples: 1. **slice()**: This method returns a new string containing a subset of characters from the original string, starting at the specified index. 2. **indexOf()**: This method finds the first occurrence of the specified value within the string and returns its index. It's worth noting that modern browsers have also introduced alternative string slicing methods, such as `String.prototype.slice()` (which is equivalent to `substr`), `String.prototype.substr()` (equivalent to `substring`), and `String.prototype.substring()` (also equivalent to `substring`). However, these are not typically used in benchmarks like this one. I hope this explanation helps you understand the benchmark test and the options compared!
Related benchmarks:
slice vs substr vs substring (try with 100)
slice vs substr vs substring (with no end index)22
slice vs substr vs substring (with no end index)222
substr vs substring (simple)
Comments
Confirm delete:
Do you really want to delete benchmark?