Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
substr vs substring (simple)
(version: 0)
Compares slice, substr and substring to each other when there is only a start index
Comparing performance of:
substr vs substring
Created:
2 years ago
by:
Registered User
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(1, 10)
substring
var result = example.substring(1, 1 + 9)
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
substr
31955842.0 Ops/sec
substring
31861638.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The benchmark being measured is "substr vs substring (simple)", which compares the performance of the `substr()` and `substring()` methods when used with only a start index. The benchmark uses a large string literal to ensure that the test cases are representative of real-world usage. **Options Compared** Two options are compared: 1. **substr()**: This method slices the input string from the specified start index up to, but not including, the end index. 2. **substring()**: This method returns a new string containing all characters of the original string that fall within the range [start index, end index). **Pros and Cons** Both methods have their advantages and disadvantages: * **substr():** + Pros: - Less overhead due to not creating a new string (only creates an integer with the length) - Can be faster for large strings + Cons: - Does not preserve the original string, which may lead to unexpected behavior in certain contexts - May have issues when dealing with negative indices or edge cases * **substring():** + Pros: - Preserves the original string, making it safer to use in many situations - Handles edge cases and negative indices more robustly + Cons: - Creates a new string, which can be slower for very large inputs **Library and Purpose** There is no explicit library mentioned in the benchmark definition. However, both `substr()` and `substring()` are part of the JavaScript standard library. **Special JS Feature/Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's normally available in JavaScript. It relies solely on the standard methods for string manipulation. **Other Alternatives** If you were to implement this benchmark yourself, here are some alternative approaches: 1. **Using the `slice()` method**: This method is similar to `substr()`, but can be more efficient due to its ability to create a new array. 2. **Using regular expressions**: Regular expressions can be used to extract substrings from strings, and may offer better performance for certain use cases. In summary, this benchmark compares the performance of two string manipulation methods in JavaScript: `substr()` and `substring()`. The `substr()` method has less overhead but may not preserve the original string, while the `substring()` method preserves the original string but creates a new one.
Related benchmarks:
slice vs substr vs substring (try with 100)
substr vs substring (with end index)
slice vs substr vs substring (with no end index)22
slice vs substr vs substring (with no end index)222
Comments
Confirm delete:
Do you really want to delete benchmark?