Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substr vs substrings
(version: 0)
Compares slice, substr and substring to each other when there is only a start index
Comparing performance of:
slice vs substring
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var example = 'there is no spoon'
Tests:
slice
var result = example.slice(4, 8)
substring
var result = example.substring(4, 8)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
slice
substring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
197847872.0 Ops/sec
substring
192581536.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is tested in the provided JSON?** The provided JSON represents a benchmark test that compares the performance of three different string slicing methods in JavaScript: `slice()`, `substring()`, and a custom implementation of "substrings" (which is not explicitly implemented in the provided code, but assumed to be a typo or an alternative name for one of the existing methods). The test creates a single string variable `example` with the value `'there is no spoon'` and then runs each of the three slicing methods on this string. The results are stored in two separate test cases: "slice" and "substring". **Options compared** The options being compared are: 1. `slice()` 2. `substring()` 3. A custom implementation of "substrings" (not explicitly implemented in the provided code) **Pros and Cons of each approach** Here's a brief analysis of each approach: 1. **`slice()`**: This is a built-in JavaScript method that creates a new string by extracting a portion of an existing string. The pros are: * Performance: `slice()` is generally faster than `substring()` because it avoids the overhead of parsing and comparing the indices. * Conciseness: `slice()` requires fewer characters to express the same logic as `substring()`. * Consistency: `slice()` has a consistent behavior across different browsers and engines. However, the cons are: * Limited control: `slice()` only returns a new string; it doesn't modify the original string. 2. **`substring()`**: This is also a built-in JavaScript method that creates a new string by extracting a portion of an existing string. The pros are: * Flexibility: `substring()` allows for more flexibility in terms of specifying both start and end indices, whereas `slice()` only takes one index as an argument. 3. **Custom "substrings" implementation**: Not implemented in the provided code, but assumed to be a typo or alternative name for one of the existing methods. **Other considerations** * The test doesn't consider the case where the start index is negative; if it were included, the results might differ significantly. * The test assumes that the input string will always have at least two characters; if this isn't guaranteed, the results might not be representative. * The browser used in the benchmark (Chrome 114) may have specific optimizations or quirks that affect the performance of these methods. **Library and purpose** None explicitly mentioned, but JavaScript built-in methods like `slice()` and `substring()` are part of the ECMAScript standard. **Special JS feature or syntax** None mentioned, as no special features or syntax are used in this benchmark.
Related benchmarks:
slice vs substring
slice vs substr vs substring with end
slice vs substr vs substring (with end index) @fran
slice vs substring (with end index)
Comments
Confirm delete:
Do you really want to delete benchmark?