Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substr vs substring
(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:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var example = 'hello world !'
Tests:
slice
var result = example.slice(0, 6)
substr
var result = example.substr(0, 6)
substring
var result = example.substring(0, 6)
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 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 136 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
94592480.0 Ops/sec
substr
110375856.0 Ops/sec
substring
108259528.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares the performance of three string slicing methods in JavaScript: `slice`, `substr`, and `substring`. The test is designed to measure the execution time of each method when only a start index is specified. **Methods Compared** 1. **`slice()`**: This method returns a new string containing all characters from the original string, starting at the specified index. 2. **`substr()`**: This method also returns a new string containing all characters from the original string, starting at the specified index. However, unlike `slice()`, `substr()` does not allow specifying an end index. The end index is assumed to be the length of the string if no second argument is provided. 3. **`substring()`**: This method returns a new string containing all characters from the original string, starting at the specified start index and ending at the specified end index. **Pros and Cons of Each Approach** 1. **`slice()`**: * Pros: Fastest execution time among the three methods due to its direct access to the string's internal buffer. * Cons: May not be compatible with all browsers or environments, as it relies on the underlying string implementation. 2. **`substr()`**: * Pros: Simple and widely supported across browsers and environments. * Cons: Slower execution time compared to `slice()`, especially for large strings. 3. **`substring()`**: * Pros: More intuitive and flexible than `substr()`, allowing the specification of both start and end indices. * Cons: May be slower than `slice()` due to its additional overhead. **Library Usage** None of the methods use a library or any external dependencies in this benchmark. The test focuses solely on the native JavaScript string slicing methods. **Special JS Features or Syntax** No special JavaScript features or syntax are used in this benchmark. **Alternative Approaches** In addition to the three standard string slicing methods, other approaches could be considered: 1. **Using a library**: A dedicated string manipulation library like jQuery or Lodash could provide optimized and feature-rich string slicing functionality. 2. **Using regular expressions**: Regular expressions can be used to extract substrings from strings, but this approach may be slower than the standard string slicing methods. 3. **Using a buffer-based implementation**: For large strings, using a buffer-based implementation might offer better performance compared to the standard string slicing methods. In conclusion, the benchmark provides a clear comparison of the performance of three native JavaScript string slicing methods: `slice()`, `substr()`, and `substring()`. While each method has its pros and cons, `slice()` is generally considered the fastest approach due to its direct access to the underlying string's internal buffer.
Related benchmarks:
slice vs substr vs substring (with end index) -x
slice vs substr vs substring with end
slice vs substr vs substring (with end index) @fran
slice vs substr vs substring (with no end index) - 2
Performance Test: substring vs substr vs slice with StartIndex
Comments
Confirm delete:
Do you really want to delete benchmark?