Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Performance Test: substring vs substr vs slice constant length
(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 string = "I am the god of hellfire, and I bring you..."
Tests:
slice
var substring = string.slice(0, 1024);
substring
var substring = string.substring(0, 1024);
substr
var substring = string.substr(0, 1024);
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:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
165930512.0 Ops/sec
substring
183833152.0 Ops/sec
substr
180550624.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. The goal of this benchmark is to compare the performance of three different string manipulation methods: `slice()`, `substring()`, and `substr()` (which is essentially an alias for `substring()`). **Options compared:** 1. `slice()` 2. `substring()` 3. `substr()` (which is equivalent to `substring()`) **Pros and Cons of each approach:** * **`slice()`**: This method returns a new string object containing the characters of an existing string, from start index (default 0) to end index (optional). It's a bit more flexible than `substring()` because it doesn't require specifying the ending index. * Pros: * More flexible with the ending index. * Can be used for more complex slicing tasks. * Cons: * Creates a new string object, which can be memory-intensive if dealing with very large strings. * **`substring()`**: This method returns a new string containing the characters of an existing string, between start and end indices (both inclusive). It's similar to `slice()`, but it doesn't create a new string object. * Pros: * More memory-efficient than `slice()` because it reuses the original string. * Simpler syntax for common use cases. * Cons: * Less flexible with the ending index (since it's inclusive). * May be slower for very large strings or complex slicing tasks. * **`substr()`** : This is equivalent to `substring()` and doesn't offer any additional benefits. In general, `slice()` provides more flexibility but comes at a memory cost, while `substring()` offers better memory efficiency at the expense of some flexibility. The choice between these methods depends on the specific use case and performance requirements. The benchmark script uses a sample string ("I am the god of hellfire, and I bring you...") to measure the execution time for each method. **Library and purpose:** None mentioned in this benchmark definition. **Special JavaScript feature or syntax:** There are no special features or syntaxes used in this benchmark (e.g., async/await, Promises, etc.). **Other alternatives:** 1. **`indexOf()` and `substring()` combination**: Instead of using `slice()` or `substring()`, you could use `indexOf()` to find the position of the start index and then use `substring()` to extract the desired substring. * Pros: * Still efficient in terms of memory usage. * Cons: * More complex syntax. 2. **Regular expressions**: If you need to perform more complex string manipulation tasks, regular expressions might be a better choice than these methods. However, they're often slower and less flexible for simple substring extraction tasks. 3. **String.prototype.slice() with a for loop**: You could use `slice()` but iterate over the sliced array manually using a `for` loop instead of checking the execution time directly.
Related benchmarks:
Performance Test: substring vs substr vs slice
Performance Test: substring vs subsstr vs slice
Performance Test: substring vs substr vs slice 1
Performance Test: substring vs substr (remove last 10 chars)
Comments
Confirm delete:
Do you really want to delete benchmark?