Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Performance Test: substring vs substr vs slice (remove last char)
(version: 0)
Comparing performance of:
slice vs substring vs substr
Created:
5 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, -1);
substring
var substring = string.substring(0, string.length - 1);
substr
var substring = string.substr(0, string.length - 1);
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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
20907486.0 Ops/sec
substring
11354052.0 Ops/sec
substr
11306389.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The test measures the performance difference between three ways to remove the last character from a string: `slice`, `substring`, and `substr`. The goal is to determine which approach is the fastest. **Options Compared** 1. **Slice**: The `slice()` method returns a new string, containing all characters from the start index (0) up to, but not including, the end index (which defaults to the end of the string). By using `-1` as the end index, we effectively remove the last character. 2. **Substring**: The `substring()` method also returns a new string, containing all characters from the start index (0) up to, but not including, the end index (which defaults to the length of the original string). 3. **Substr**: The `substr()` method returns a new string, containing all characters from the start index (0) up to, but not including, the end index (which defaults to 1). Again, by using `-1` as the end index, we remove the last character. **Pros and Cons of Each Approach** * **Slice**: Pros: + Generally faster than `substring` and `substr`. + Returns a new string, which can be beneficial if you need to work with multiple strings. * Cons: * Creates an additional object in memory, which can be a concern for very large datasets. * **Substring**: Pros: + Can be more efficient than `slice` when the original string is very long and there's no performance penalty from creating an intermediate result. + Still creates an additional object in memory. * Cons: * Slower than `slice`. * **Substr**: Pros: + Even slower than `substring`. + Can be problematic if you're working with strings that don't have enough characters to reach the default end index of 1. **Library Used** None. The benchmark only uses JavaScript's built-in methods and variables. **Special JS Features or Syntax** There are no special features or syntax used in this benchmark. The code is straightforward and simple, making it easy to understand for anyone familiar with basic JavaScript programming. **Other Alternatives** If you wanted to remove the last character from a string without using any of these three methods, you could use: * `string.slice(0, -1)` (same as `slice`) * `string.substring(0, string.length - 1)` (same as `substring`) * `string.substr(0, string.length - 1)` (same as `substr`) However, these alternatives do not provide any performance benefits over the original three methods. I hope this explanation helps you understand what's going on in this benchmark!
Related benchmarks:
Performance Test: substring vs subsstr vs slice
Performance Test: substring vs substr vs slice constant length
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?