Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Performance Test: substring vs subsstr vs slice
(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 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbring you..."
Tests:
slice
var substring = string.slice(17, 25);
substring
var substring = string.substring(17, 25);
substr
var substring = string.substr(17, 25);
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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0
Browser/OS:
Firefox 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
1074014080.0 Ops/sec
substring
1172214016.0 Ops/sec
substr
1037505088.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its test cases. **Benchmark Overview** The benchmark is designed to compare the performance of three different methods for extracting a substring from a string: `slice()`, `substring()`, and `substr()`. **Test Cases** Each test case consists of a JavaScript code snippet that extracts a substring from a predefined string using one of the three methods. The substrings to be extracted are fixed (from index 17 to 25) in all cases. Here's a brief explanation of each method: 1. **slice()**: `slice(start, end)` returns a new string containing the characters from `start` to `end-1`. 2. **substring()**: `substring(start, end)` returns a new string containing the characters from `start` to `end-1`. This method is similar to `slice()`, but it's specifically designed for extracting substrings. 3. **substr()**: `substr(index, length)` returns a new string containing the characters from `index` to `index+length-1`. **Options Compared** The benchmark compares the performance of these three methods in terms of execution time. **Pros and Cons of Each Approach** * **slice() and substring():** * Pros: * Both are widely supported by modern browsers. * They have a simple syntax and are easy to use. * Cons: * `substring()` is specifically designed for extracting substrings, so it might be optimized better than `slice()`. * **substr():** * Pros: * It's often faster than `substring()` because it uses an internal buffer to store the result, which can reduce memory allocation and copying overhead. * Cons: * It's less commonly used than `slice()` or `substring()`, so its performance might be less consistent across different browsers. **Library Used** None of these methods rely on any external libraries. They are built-in JavaScript functions. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark. **Other Alternatives** If you wanted to extract a substring from a string, other alternatives could include using regular expressions (e.g., `string.match()` followed by array indexing) or string manipulation libraries like String.prototype.split() and String.prototype.join(). However, these approaches are not as efficient as the built-in `slice()`, `substring()`, and `substr()` methods. Here's an example of how you could use the other alternatives: ```javascript // Using regular expressions: var substring = string.match(/.{17,25}/)[0]; // Or using String.prototype.split(): var parts = string.split(''); var substring = parts[17] + parts.slice(18, 25).join(''); // Or using String.prototype.indexOf() and array slicing: var start = string.indexOf('...'); // '...' is the part of the original string that you want to slice off. var end = start + 8; // The new index should be 8 characters after the start index. var substring = string.slice(start, end); ```
Related benchmarks:
Performance Test: substring vs substr vs slice
Performance Test: substring vs substr vs slice constant length
Performance Test: substring vs substr (remove last 10 chars)
JS substring vs slice
Comments
Confirm delete:
Do you really want to delete benchmark?