Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substr vs substring to truncate string
(version: 0)
Compares slice, substr and substring to each other when the start index is 0
Comparing performance of:
slice vs substr vs substring
Created:
6 years ago
by:
Registered User
Go to the latest result
Script Preparation code:
var example = 'there is no spoon'
Tests:
slice
var result = example.slice(0,10)
substr
var result = example.substr(0,10)
substring
var result = example.substring(0,10)
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
substr
171.9M/s
slice
166.1M/s
substring
92.3M/s
View exact numbers
Test name
Executions per second
✓
substr
171,870,928 Ops/sec
slice
166,101,792 Ops/sec
substring
92,257,944 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and the pros and cons of each approach. **What is being tested?** The benchmark is designed to compare three different string manipulation methods in JavaScript: `slice()`, `substr()`, and `substring()`. The test case uses a fixed string `example = 'there is no spoon'`. **Options compared** * **Slice**: `var result = example.slice(0,10)` * This method creates a new string object containing the characters of `example` from index 0 up to (but not including) index 10. * The pros: efficient for large strings, returns a new string without modifying the original, and has fewer dependencies compared to substr and substring methods. * The cons: requires two parameters: start index and length. If not provided, slice throws an error if it is called with zero arguments. * **Substr**: `var result = example.substr(0,10)` * This method extracts a section of a string. It returns the characters of `example` starting at index 0 up to (but not including) index 10. * The pros: less parameters compared to slice and substring methods, it's faster than slice in practice because of how browsers are optimized to handle substr calls. * The cons: this method is slower than the slice method in most cases due to browser optimizations. * **Substring**: `var result = example.substring(0,10)` * This method creates a new string object containing the characters of `example` from index 0 up to (but not including) index 10. * The pros: similar to slice in terms of efficiency and has fewer dependencies compared to substr. It's actually faster than substr because it is called less often. * The cons: requires two parameters, similar to the slice method, but with a different name. **Other considerations** * **Library:** None * **Special JS feature or syntax**: None The benchmark provides results for the three methods: | Test Name | Browser | DevicePlatform | OperatingSystem | ExecutionsPerSecond | | --- | --- | --- | --- | --- | | slice | Chrome 108 | Desktop | Linux | 5242033.0 | | substr | Chrome 108 | Desktop | Linux | 5233290.0 | | substring | Chrome 108 | Desktop | Linux | 4146559.75 | **Pros and Cons of Each Approach:** 1. **Slice** * Pros: * More efficient for large strings. * Returns a new string without modifying the original string. * Fewer dependencies compared to substr and substring methods. * Cons: * Requires two parameters (start index and length). 2. **Substr** * Pros: * Less parameters compared to slice and substring methods. * Faster than slice in practice because of browser optimizations. * Cons: * Slower than the slice method in most cases due to browser optimizations. 3. **Substring** * Pros: * Similar to slice in terms of efficiency. * Fewer dependencies compared to substr. * Faster than substr because it is called less often. **Alternatives** * Other string manipulation methods like `indexOf()`, `lastIndexOf()`, or regular expressions (`RegExp`) are not being tested. * There are other libraries like Lodash, which may offer more efficient implementations for these functions.
Related benchmarks
slice vs substring (with no end index)
slice vs substring (with end index)
Comments
Confirm delete:
Do you really want to delete benchmark?