Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf but with lowercase when checking index
(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:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexStart = /^test/i; window.match = 'test'; window.matchLength = window.match.length; 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.toLowerCase().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.toLowerCase().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):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided benchmark is designed to compare the performance of three different string matching methods: regular expressions (regex), `startsWith`, and `indexOf` with a case-insensitive comparison using `.toLowerCase()`. **Script Preparation Code** Before running each test, the script prepares the following variables: * `window.regexStart`: a regex pattern (`/^test/i`) that matches the string "test" in a case-insensitive manner. * `window.match`: the target string to be matched ("test"). * `window.matchLength`: the length of the target string (1). * `data`: an array of 100,000 random strings generated using a custom function `makeRandomString(len)`. * `TOTAL_STRINGS`: the total number of strings in the `data` array. **Individual Test Cases** There are three test cases: ### regex start This test case uses the `window.regexStart` pattern to match each string in the `data` array. The regex pattern is designed to be case-insensitive, but it's not explicitly specified in the benchmark definition. Pros: Regular expressions can provide more flexibility and power when working with strings, including support for complex patterns, capturing groups, and Unicode characters. Cons: Regular expressions can be slower than other methods due to their complexity and the overhead of compiling the pattern. ### startsWith This test case uses the `startsWith` method to check if each string in the `data` array starts with the target string "test" (case-insensitive). Pros: The `startsWith` method is a built-in method that can provide fast performance, as it only needs to compare a single character at a time. Cons: This method may not work correctly for strings containing Unicode characters or non-ASCII characters. ### indexOf start This test case uses the `indexOf` method with `.toLowerCase()` to find the index of each string in the `data` array that starts with "test" (case-insensitive). Pros: The `indexOf` method can provide fast performance, as it only needs to iterate through the string until finding the target character. Cons: This method may not work correctly for strings containing Unicode characters or non-ASCII characters. **Library and Special Features** The benchmark uses a custom JavaScript function `makeRandomString(len)` to generate random strings. There are no special features used in this benchmark that require explicit mention. **Other Alternatives** If you were to reimplement this benchmark, you could also consider using other string matching methods, such as: * `includes`: a new method introduced in ECMAScript 2017 (ES2017) that checks if a string contains a specified value. * `match`: another method that can be used with regex patterns to search for matches in a string. * `replaceAll`: a method that replaces all occurrences of a specified pattern in a string. Keep in mind that the performance of these methods may vary depending on the specific use case and requirements.
Related benchmarks:
Some vs. Filter vs. indexOf vs findIndex
Some vs indexOf vs Includes vs Find
Some vs. Filter vs. indexOf vs. includes (string version)
RegEx.exec vs StrRaasdhakshjding.match
DTMF: array includes vs regex
Comments
Confirm delete:
Do you really want to delete benchmark?