Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf by Jarvis
(version: 0)
fork of https://www.measurethat.net/Benchmarks/Show/975/11/regex-vs-indexof-vs-startswith-vs-substr
Comparing performance of:
regex start vs startsWith vs indexOf start
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexStart = /^not/; window.match = 'not'; 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:
regex start
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regexStart; while (x < TOTAL_STRINGS) { const str = data[x]; regex.test(str); x += 1; }
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) === 0; x += 1; }
indexOf start
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 (3)
Previous results
Fork
Test case name
Result
regex start
startsWith
indexOf start
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 to compare the performance of different approaches on various platforms. The provided benchmark definition json represents a test case with three individual tests: "regex start", "startsWith", and "indexOf start". We'll break down what's tested, describe the options compared, pros and cons of each approach, and discuss other considerations. **Tested Options** 1. **Regex Start**: This option uses the `test()` method of a regular expression object (`window.regexStart`) to check if a given string matches a certain pattern. 2. **startsWith**: This option uses the `startsWith()` method of a string object (`window.match`) to check if a string starts with a certain prefix. 3. **IndexOf Start**: This option uses the `indexOf()` method of a string object (`window.match`) to find the index of the first occurrence of a certain substring. **Options Compared** The three options are compared in terms of their performance (measured by executions per second) on a desktop platform with Firefox 88 browser. **Pros and Cons** * **Regex Start**: + Pros: Can match any pattern, not limited to prefixes. + Cons: Can be slower due to the complexity of regular expressions, especially for complex patterns. * **startsWith**: + Pros: Fast and efficient for prefix matching, as it only checks a few characters. + Cons: Limited to checking prefixes only, may not match more complex patterns. * **IndexOf Start**: + Pros: Faster than Regex Start, but still slower than StartsWith due to the overhead of searching for the index. + Cons: May be less efficient for prefix matching compared to StartsWith. **Library and Special JS Features** * The `window.regexStart` object uses a regular expression pattern (`/^not/`) that is not a standard JavaScript regex pattern. This suggests that MeasureThat.net might be testing the performance of custom regex patterns or patterns that are specific to the Firefox browser. * The `window.match` object uses the `startsWith()` method, which was introduced in ECMAScript 2022 (ES12). **Considerations** * The benchmark definition json uses a fixed string length (`20`) for generating random strings, which might not be representative of real-world scenarios where string lengths can vary. * The number of iterations (`TOTAL_STRINGS = 100000`) is relatively low compared to other benchmarks, which may affect the accuracy of the results. * MeasureThat.net provides detailed information about the browser and platform used, which can help identify performance differences due to hardware or software factors. **Alternative Approaches** Other approaches for prefix matching could include: * Using a simple substring comparison (e.g., `str.includes(match)` or `str.localeCompare(match)`) * Using a library like `lodash` or `underscore`, which provide optimized implementations of prefix matching methods * Using a different JavaScript engine or browser, to see if performance differences are due to specific implementation details. Keep in mind that these alternative approaches may have different trade-offs in terms of complexity, performance, and accuracy.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf simpler
JS Regex vs .startsWith vs .indexOf Jan
JavaScript: Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith
Comments
Confirm delete:
Do you really want to delete benchmark?