Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substr vs substring (with start and end index, and with emoji)
(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:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var example = '👶'.repeat(10000)
Tests:
slice
var result = example.slice(100,1000)
substr
var result = example.substr(100,1000)
substring
var result = example.substring(100,1000)
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:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
16725559.0 Ops/sec
substr
16621867.0 Ops/sec
substring
16912784.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! The provided JSON represents a benchmarking test that compares the performance of three string slicing methods: `slice()`, `substr()`, and `substring()` in JavaScript. These methods are used to extract parts of a string. **Methods being compared:** 1. `slice()`: Returns a new string containing a subset of characters from the original string, starting at the specified index (`start`) and ending at the specified index (`end`). In this benchmark, only the start index is provided. 2. `substr()`: Similar to `slice()`, but it returns a substring without creating a new object. However, its performance can vary depending on the browser and JavaScript engine used. 3. `substring()`: Also similar to `slice()`, but with an optional second argument that specifies the length of the substring. **Pros and Cons:** 1. **`slice()`**: Pros: * Fastest among the three methods (on average). * Creates a new string object, which can be beneficial for certain use cases. Cons: * Can be slower than `substr()` or `substring()` in some browsers. 2. **`substr()`**: Pros: + Faster than `slice()` and `substring()` in some browsers. Cons: + Performance can vary depending on the browser and JavaScript engine used. + May create a new object, which can be detrimental for performance-critical code. 3. **`substring()`**: Pros: + Similar performance to `substr()`, making it a safe choice. Cons: + Can be slower than `slice()` in some browsers. **Other considerations:** 1. The benchmarking test uses a large string (`👶`.repeat(10000)) to ensure that the methods are exercised thoroughly. 2. The test only compares the slicing methods with the start index, while ignoring the end index. **Library and JavaScript feature used:** None mentioned in the provided JSON. However, it's worth noting that the `repeat()` method is a built-in JavaScript method for creating repeated strings. **Special JS features or syntax:** None explicitly mentioned. The benchmarking test focuses on the slicing methods' performance without any special features or syntax. **Alternatives:** If you're interested in exploring alternative string manipulation methods, here are some options: 1. `indexOf()` and `slice()` with negative indices. 2. Using regular expressions to extract parts of a string. 3. Utilizing libraries like Lodash or Underscore.js for functional programming techniques. 4. Employing more advanced string manipulation methods, such as slicing using Unicode code points. For most use cases, the original slicing methods (`slice()`, `substr()`, and `substring()`) will suffice. However, if you're working on a performance-critical project or need to optimize specific strings, exploring alternative approaches might be worth considering.
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
Comments
Confirm delete:
Do you really want to delete benchmark?