Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startsWith vs include vs substring
(version: 0)
Comparing performance of:
JS startsWith vs JS includes vs JS substring
Created:
5 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 includes
window.allStrings.forEach(s => { s.includes(window.prefix); })
JS substring
window.allStrings.forEach(s => { s.substring(0, window.prefix.length) === 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 includes
JS substring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month 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
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JS startsWith
22262.4 Ops/sec
JS includes
32133.3 Ops/sec
JS substring
214525.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of three string operations: `startsWith`, `includes`, and `substring`. The script generates an array of 2000 random strings, which are used to test these operations. The benchmark is designed to measure the execution time of each operation on a large dataset. **Options Compared** 1. **`startsWith`**: This method checks if a string starts with a given prefix. 2. **`includes`**: This method checks if a string contains a given substring. 3. **`substring`**: This method extracts a part of a string, starting from a specified index and with a specified length. **Pros and Cons of Each Approach** 1. **`startsWith`**: * Pros: Fast, since it only needs to check the first character of the string against the prefix. * Cons: May not be suitable for large strings or prefixes, as it may cause unnecessary work. 2. **`includes`**: * Pros: Flexible and efficient, as it can search for any substring in the string. * Cons: May be slower than `startsWith` for small prefixes, since it needs to search through the entire string. 3. **`substring`**: * Pros: Efficient, since it only extracts a fixed-size part of the string. * Cons: May not be suitable for large strings or extracting long substrings. **Library and Special JS Feature** None of these operations use any libraries or special JavaScript features that are not widely supported. However, some modern browsers may optimize or implement these operations using more efficient algorithms. **Other Considerations** * The benchmark uses a random string generator to create the test data, which helps to eliminate bias towards specific strings. * The script uses a fixed prefix and length for the generated strings, which simplifies the comparison between the three operations. * The benchmark measures execution time per second (ExecutionsPerSecond), which provides a rough estimate of performance. **Alternative Benchmarks** To compare these operations, other benchmarks might focus on different aspects, such as: 1. **String similarity**: Measuring the similarity between two strings using metrics like Levenshtein distance or Jaro-Winkler distance. 2. **Regular expressions**: Comparing the performance of regular expression-based string matching (e.g., `RegExp.test()`). 3. **Hash-based lookup**: Using hash tables to store and look up strings, which can provide faster lookup times for certain use cases. Keep in mind that different benchmarks may prioritize specific aspects of performance, such as raw execution speed or memory usage.
Related benchmarks:
.startsWith vs .charAt for single character
.startsWith vs .substr
startsWith vs substr
startsWith vs slice vs substring
Comments
Confirm delete:
Do you really want to delete benchmark?