Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substring vs substr
(version: 0)
Comparing performance of:
slice vs substring vs substr
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var example = 'abcdefghijklmnopqrstuvwxyz,./;-==;[]5446621330789'.repeat(1000)
Tests:
slice
var slice = example.slice(5000);
substring
var substring = example.substring(5000);
substr
var substr = example.substr(5000)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
slice
substring
substr
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
183401424.0 Ops/sec
substring
186087440.0 Ops/sec
substr
177356224.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark compares the performance of three string slicing methods in JavaScript: `slice()`, `substring()`, and `substr()`. The test case uses a large string (`example`) repeated 1000 times to simulate a real-world scenario. Each method is tested with a specific starting index (5000). **String Slicing Methods** 1. **`slice()`**: This method returns a new string that includes the specified start and end indices. It's used as `example.slice(5000)`. 2. **`substring()`**: This method returns a new string that includes the characters between the specified start and end indices (exclusive). It's used as `example.substring(5000)`. 3. **`substr()`**: This method returns a new string that includes the specified number of characters starting from the specified index. It's used as `example.substr(5000)`. **Comparison and Pros/Cons** All three methods have their pros and cons: * **`slice()`**: * **Pros:** Fast, efficient, and widely supported by modern browsers. * **Cons:** Creates a new string object, which can lead to performance issues if the original string is large. * **`substring()`**: * **Pros:** Also fast and efficient but creates a new string object as well. It's often preferred over `slice()` in older browsers or environments where `slice()` is not supported. * **Cons:** Same as `slice()`: creates a new string object, leading to potential performance issues with large strings. * **`substr()`**: * **Pros:** Some older JavaScript engines (e.g., V8 before 2015) were faster than the other two methods due to its ability to optimize certain cases. However, modern browsers have largely abandoned `substr()` in favor of `slice()` or `substring()`. * **Cons:** Generally slower and less efficient than `slice()` and `substring()`, especially for large strings. **Library and Special JS Feature** None of the test cases use a library or any special JavaScript features beyond standard syntax. The benchmark only focuses on the performance differences between these three string slicing methods. **Other Alternatives** Some alternative approaches to string slicing include: * Using `indexOf()` with offset values to calculate slice positions. * Implementing custom string slicing algorithms (e.g., using bitwise operators). * Utilizing libraries like ICU or String-slicing plugins for optimized performance. Keep in mind that these alternatives may have different trade-offs, such as additional complexity, compatibility issues, or overhead.
Related benchmarks:
slice vs substring remove last char
Performance Test: substring vs substr vs slice constant length
slice vs substring from end
Performance Test: substring vs substr vs slice 1
slice vs substr vs substring 122459
Comments
Confirm delete:
Do you really want to delete benchmark?