Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.indexOf vs .startsWith - cccccccccccccccccccc
(version: 0)
Testing some things
Comparing performance of:
startsWith vs .indexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.match = 'test'; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; 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; } while (data.length < TOTAL_STRINGS) { data.push(makeRandomString(getRandomInt(20))); }
Tests:
startsWith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.startsWith(match); x += 1; }
.indexOf
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.indexOf(match) === 0; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
startsWith
.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents two benchmark definitions, which compare the performance of `str.startsWith()` and `str.indexOf(match)` in different scenarios. **Benchmark 1: .indexOf vs .startsWith - cccccccccccccccccccc** This benchmark creates an array of 100,000 random strings with a fixed length (20 characters) and uses them to test both `.indexOf` and `.startsWith`. The `getRandomString()` function generates each string by concatenating random characters from a predefined character set. The script preparation code sets up the necessary variables: * `window.match = 'test';`: Assigns a value to `window.match`, which is used as the search term. * `var data = window.data = [];`: Initializes an empty array, which will store the generated random strings. * `const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";`: Defines a string of possible characters used in generating random strings. * `var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000;`: Sets the number of strings to generate. The HTML preparation code is empty, but it might be used for rendering the benchmark results. **Benchmark Options** Two options are compared: 1. `.indexOf(match) === 0`: * Pros: This approach ensures that `match` must appear at the beginning of the string. * Cons: If `match` appears anywhere in the string, this condition will be false. 2. `str.startsWith(match)`: * Pros: This approach checks if `match` appears at the beginning of the string, and also allows for cases where `match` is not present in the string. * Cons: If `match` does not appear at the beginning of the string, this condition will be false. **Library** The `window.data` array is used to store generated random strings. This suggests that the test case uses a custom data storage mechanism. **Special JS Feature/Syntax** None mentioned in the provided JSON. **Other Alternatives** Alternative implementations for `.indexOf()` and `.startsWith()` could be: * Using regular expressions (`/match/`) instead of `indexOf()` or `startsWith()`. * Implementing these methods using a more efficient algorithm, such as a simple prefix tree data structure. * Comparing performance with other JavaScript engines or versions. Note that the actual implementation details of these alternatives are not provided in the JSON and would require additional analysis to determine their feasibility.
Related benchmarks:
.startsWith vs .charAt for single character
IndexOf vs startwith
indexOf vs startsWith
.startsWith vs .charAt vs str[0] for single character
Comments
Confirm delete:
Do you really want to delete benchmark?