Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substr vs substring (with an end index)
(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:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var example = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
Tests:
slice
var result = example.slice(10, example.length - 2)
substr
var result = example.substr(10, example.length - 2)
substring
var result = example.substring(10, example.length - 2)
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Overview** The benchmark compares three JavaScript methods to extract a substring from a given string: `slice()`, `substr()`, and `substring()`. **Methods Compared** 1. **`slice()`**: The `slice()` method returns a new string that contains a subset of characters from the original string, starting from the specified start index up to (but not including) the end index. 2. **`substr()`**: The `substr()` method also returns a substring, but it's not as widely supported as `slice()` or `substring()`. It was used before the introduction of `slice()` and `substring()` in older browsers. 3. **`substring()`**: The `substring()` method is similar to `slice()` but uses an optional second argument (end index) that defaults to the end of the string if omitted. **Pros/Cons of Each Approach** 1. **`slice()`**: * Pros: Widely supported, efficient, and flexible. * Cons: May not be necessary due to its similarity to `substring()`. 2. **`substr()`**: * Pros: None notable. * Cons: Less efficient and less widely supported than `slice()` or `substring()`. 3. **`substring()`**: * Pros: More flexible than `slice()` due to the optional second argument (end index). * Cons: Still not as widely supported as `slice()`. **Library Used** None of the benchmark code uses a library, so we don't need to discuss any specific libraries in this context. **Special JS Feature or Syntax** There's no special JavaScript feature or syntax being used in these benchmarks. They're standard JavaScript methods that are compared directly in the code. **Benchmark Results Analysis** The latest benchmark results show that: 1. `slice()` performs best, with an average of approximately 7876707.5 executions per second. 2. `substr()` is slower than both `slice()` and `substring()`, with an average of approximately 7757570.0 executions per second. 3. `substring()` is slower than `slice()` but faster than `substr()`, with an average of approximately 7807562.0 executions per second. **Alternatives** Other alternatives for extracting substrings from a string in JavaScript include: 1. Using the `indexOf()` method to find the index of the start character and then using `slice()` or `substring()`. 2. Using regular expressions, such as `RegExp.prototype.exec()`. 3. Implementing a custom substring extraction function. However, these alternatives are not part of the standard JavaScript API and may have varying degrees of performance and compatibility across browsers.
Related benchmarks:
slice vs substr vs substring (with end index & large string)
slice vs substr vs substring (with no end index) and long text
slice vs substring (long string)
Array from vs string split with large strings
Comments
Confirm delete:
Do you really want to delete benchmark?