Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startsWith vs include
(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 = "%%CHAT_TRANSCRIPT%%"; 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("%%CHAT_TRANSCRIPT%%\n\n" + 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 year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JS startsWith
19880.9 Ops/sec
JS includes
61883.5 Ops/sec
JS substring
23994.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript benchmarks! **Benchmark Overview** The provided JSON represents a benchmark test suite that compares the performance of three different string manipulation approaches: `startsWith`, `includes`, and `substring`. The test generates a large number of random strings (1000 characters each) and iterates through them, applying each of these operations on the prefix string. The goal is to determine which approach is the fastest. **Options Compared** The three options being compared are: 1. **`startsWith`**: Returns `true` if the string starts with the specified prefix. 2. **`includes`**: Returns `true` if the string includes the specified prefix (case-sensitive). 3. **`substring`**: Returns `true` if the first `n` characters of the string are equal to the specified prefix. **Pros and Cons** Here's a brief summary of each approach: 1. **`startsWith`**: * Pros: Efficient, as it only requires a single comparison. * Cons: May not be suitable for case-insensitive searches or when the prefix is not a substring of the input string. 2. **`includes`**: * Pros: Case-sensitive and flexible, making it suitable for various use cases. * Cons: Slower than `startsWith`, as it requires a full string search. 3. **`substring`**: * Pros: Can be faster for large strings, as it only requires comparing the first few characters. * Cons: May not be suitable when the prefix is not a substring of the input string. **Library and Special JS Features** None of the provided benchmark code utilizes any external libraries or special JavaScript features beyond the standard `Math` library for random number generation. **Other Alternatives** If you're interested in exploring alternative approaches, consider these: * **Regex-based searches**: You can use regular expressions to improve performance for case-insensitive searches. * **Character class comparisons**: Using character classes (e.g., `[abc]`) can provide a faster alternative to `startsWith` and `includes`. * **Native string methods**: Some browsers may have optimized native string methods, such as `String.prototype.startsWith()` or `String.prototype.includes()`, which might be worth investigating. **Test User Considerations** When interpreting the benchmark results: * Be aware of the device platform (Desktop vs. Mobile) and browser version. * Note that the execution rate is reported per second, which can indicate performance variations between runs. * The raw UA string may provide additional context about the user's environment or browser configuration. By understanding these aspects of the benchmark test suite, you'll be better equipped to make informed decisions when selecting a suitable approach for your specific use case.
Related benchmarks:
startsWith vs include vs substring
.startsWith vs .charAt vs str[0] for single character
startsWith vs substr
startsWith vs slice vs substring
Comments
Confirm delete:
Do you really want to delete benchmark?