Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[JS] Substring vs Slice
(version: 1)
Comparing performance of:
substr vs slice
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
var strIn = '#Allocation'; var strOut = '';
Tests:
substr
strOut = strIn.substr(1);
slice
strOut = strIn.slice(1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
substr
slice
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
14 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
substr
9376124.0 Ops/sec
slice
9604727.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark presented is designed to compare the performance of two string manipulation methods in JavaScript: `substr` and `slice`. Both of these methods are used to extract a portion of a string, but they have slightly different behaviors and use cases. ### Test Cases Explained 1. **`substr`**: - **Code**: `strOut = strIn.substr(1);` - **Description**: The `substr()` method returns a substring starting from the specified index (in this case, index `1`, which corresponds to the character after the `#` in `'#Allocation'`) to the end of the string. This method is considered legacy and is generally recommended for use only if compatibility with older browsers is required. 2. **`slice`**: - **Code**: `strOut = strIn.slice(1);` - **Description**: The `slice()` method extracts a section of a string and returns it as a new string, without modifying the original string. It also accepts a second parameter that indicates the end index (optional); however, it will take everything from the start index to the end if that parameter is omitted, as in this case. ### Performance Comparison From the benchmark results: - `slice` achieved a performance of **16,785,142** executions per second. - `substr` achieved a performance of **16,738,382** executions per second. This shows that `slice` is marginally faster than `substr` in this particular test run, though both methods perform quite similarly. The performance difference worth mentioning may vary depending on the JavaScript engine, the environment where the code is executed, and the input size. ### Pros and Cons of `substr` vs. `slice` #### `substr`: - **Pros**: - Straightforward for extracting part of a string when the number of characters to extract is known. - **Cons**: - Considered a legacy method; not recommended for use in modern JavaScript development. - Can lead to confusion due to its non-standard behavior in handling negative indices. #### `slice`: - **Pros**: - More versatile than `substr`; capable of accepting negative indices, which count back from the end of the string. - The standard method and widely used throughout modern JavaScript development. - **Cons**: - Slightly more complex due to additional parameters, though this can also be seen as an advantage. ### Other Considerations - **Library Usage**: In this benchmark, no external libraries are utilized. The testing is purely between built-in JavaScript methods. - **Special Features**: This benchmark does not test any advanced JavaScript features or syntactic elements; it solely examines the performance differences of two standard string methods. ### Alternatives 1. **`substring()`**: Similar to both methods, but it does not accept negative indices and can produce different results compared to both methods when dealing with out-of-bound values. 2. **Third-party libraries**: Libraries such as Lodash provide utility functions for more complex string manipulations. However, for simple substring extraction, using native methods is generally more efficient. 3. **Template Literals**: For string composition rather than extraction, template literals (using backticks ``) can be used, but they are not alternatives to `substr` or `slice` regarding extracting parts of a string. In summary, while both `substr` and `slice` can be used for substring extraction in JavaScript, `slice` is generally preferred due to its flexibility and performance benefits in modern JavaScript development.
Related benchmarks:
string lookup
Performance Test: substring vs substr vs slice (remove last char)
Performance Test: substring vs substr vs slice1
Performance Test: indexOf + slice vs split
substring vs slice
Performance Test: substring vs substr vs slice 1
replace vs. slice
String indexer vs substring
CUSTOM substring vs slice
Comments
Confirm delete:
Do you really want to delete benchmark?