Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startsWith vs slice vs substring
(version: 0)
Comparing performance of:
JS startsWith vs JS slice vs JS substring
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.prefix = "%%SOME_PREFIX%%"; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for( var i=0; i < len; i++ ) { text += possible.charAt(getRandomInt(possible.length)); } return text; } window.allStrings = []; for (let i = 0; i < 1000; i++) { allStrings.push(window.prefix + makeRandomString(1000)); allStrings.push(makeRandomString(1000)); }
Tests:
JS startsWith
window.allStrings.forEach(s => { s.startsWith(window.prefix); })
JS slice
let len = window.prefix.length; window.allStrings.forEach(s => { s.slice(0, len) === window.prefix; })
JS substring
let len = window.prefix.length; window.allStrings.forEach(s => { s.substring(0, len) === window.prefix; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
JS startsWith
JS slice
JS substring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 YaBrowser/23.7.1.1215 Yowser/2.5 Safari/537.36
Browser/OS:
Yandex Browser 23 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JS startsWith
3686.5 Ops/sec
JS slice
3703.4 Ops/sec
JS substring
3484.8 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, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark compares three different ways to check if a string starts with a fixed prefix: 1. `startsWith()` 2. `slice()` with a length argument 3. `substring()` with a start index The benchmark generates 2000 random strings, each containing the prefix and some other characters, and then runs each of the three checks on each string. **Library Used** In this benchmark, the `YaBrowser/23` browser is using a custom implementation of the `startsWith()` method. This library is not part of the standard JavaScript API, but rather a modified version provided by the browser itself. The purpose of this library is to compare the performance of the built-in `startsWith()` method against two alternative approaches. **Options Compared** Here's a brief overview of each option: 1. **`startsWith()`**: This is the most straightforward way to check if a string starts with a prefix. It uses a simple loop to iterate over the characters of the input string and checks if they match the prefix. 2. **`slice()` with length argument**: Instead of using `startsWith()`, this approach slices the input string up to the length of the prefix, and then compares it with the prefix. 3. **`substring()` with start index**: This approach uses `substring()` to extract a subset of the input string starting from an arbitrary index (in this case, 0), and then compares it with the prefix. **Pros and Cons** Here are some pros and cons for each approach: 1. **`startsWith()`**: * Pros: Simple, efficient, and widely supported. * Cons: May not perform well on very large strings or in environments where string caching is limited. 2. **`slice()` with length argument**: * Pros: Can be faster than `startsWith()` for very large strings, since it avoids the overhead of a loop. * Cons: Requires precise control over the substring boundaries, and may not perform well on strings with many duplicate characters. 3. **`substring()` with start index**: * Pros: Allows for more flexibility in choosing the start index, which can be useful for certain use cases. * Cons: May require more memory to store the intermediate result (the sliced string), which can impact performance. **Special JS Features and Syntax** There is no special JavaScript feature or syntax used in this benchmark. The code relies solely on standard JavaScript language features and built-in functions. **Alternative Approaches** Here are some alternative approaches that could be explored: 1. **Regular expressions**: Using regular expressions to match the prefix at the beginning of the string could provide a more efficient solution than the `startsWith()` method. 2. **Buffer-based approach**: Implementing a custom buffer-based approach using native JavaScript types (e.g., `Uint8Array`) could potentially outperform the current implementation. 3. **Use of caching or memoization**: Caching or memoizing the results of string slicing and substring operations could reduce overhead and improve performance. These alternatives would require more complex implementations, but could provide significant performance improvements in certain scenarios. Keep in mind that this benchmark is focused on comparing specific string manipulation techniques, rather than evaluating the overall efficiency or performance of JavaScript.
Related benchmarks:
.startsWith vs .charAt for single character
startsWith vs include vs substring
.startsWith vs .charAt vs str[0] for single character
.startsWith vs .charAt vs .charCodeAt for single character
Comments
Confirm delete:
Do you really want to delete benchmark?