Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Performance Test: substring vs substr vs slice
(version: 0)
Comparing performance of:
slice vs substring vs substr
Created:
7 years ago
by:
Registered User
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(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:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
188065824.0 Ops/sec
substring
191900032.0 Ops/sec
substr
196371168.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of three different methods for extracting a substring from a string in JavaScript: 1. `slice()` 2. `substring()` 3. `substr()` These methods are used to extract a portion of a string, but they differ in their implementation and usage. **Options Compared** Here's a brief overview of each method: * `slice(start, end)`: Returns a new string that includes all characters from the `start` index up to (but not including) the `end` index. If `start` or `end` is negative, it counts from the start/end of the string respectively. * `substring(start, end)`: Similar to `slice()`, but returns a new string with the specified length, without any additional characters. * `substr(index, length)`: Returns a new string that includes all characters from the `index` position up to (but not including) the `index + length` position. **Pros and Cons** Here are some pros and cons of each method: * `slice()`: + Pros: Fastest, most efficient way to extract a substring. It uses native JavaScript functions. + Cons: May not be suitable for very large strings or negative indices. * `substring()`: + Pros: More readable than `slice()` and allows for specifying the length of the substring. + Cons: May be slower than `slice()` due to additional checks. * `substr()`: + Pros: Similar to `substring()`, but more concise. + Cons: Less readable, and some older browsers may not support it. **Library Usage** None of the provided methods rely on external libraries. **Special JS Features or Syntax** There are no specific JavaScript features or syntax being tested in this benchmark. However, the use of negative indices (`slice(-17, -25)` is not shown in the test cases, but if present, it would be handled by the `slice()` method). **Other Alternatives** If you need to extract a substring from a string, some other alternatives include: * Using regular expressions (`/regex/.exec(string)`) * Using the `indexOf()` and `substring()` methods together (`string.substring(string.indexOf('substring') + 1))` * Using a library like jQuery's `.substr()` method Note that these alternatives may have different performance characteristics and use cases. In summary, this benchmark measures the performance of three different methods for extracting a substring from a string in JavaScript. The `slice()` method is generally considered the fastest and most efficient way to achieve this, but it requires careful handling of negative indices.
Related benchmarks:
Performance Test: substring vs subsstr 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?