Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Multiple JS Regex vs .startsWith vs .indexOf as anonymous function
(version: 2)
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 startsWith anonymous vs indexOf start vs indexOf start anonymous vs indexOf start named
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexStart = /^(aaa|bbb|ccc|ddd|eee|fff)/; window.match = ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'] ; 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]; if (str.startsWith(match[0]) || str.startsWith(match[1]) || str.startsWith(match[2]) || str.startsWith(match[3]) || str.startsWith(match[4]) || str.startsWith(match[5])) {}; x += 1; }
startsWith anonymous
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]; match.some(m => str.startsWith(m)); 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]; if(str.indexOf(match[0]) === 0 || str.indexOf(match[1]) === 0 || str.indexOf(match[2]) === 0 || str.indexOf(match[3]) === 0 || str.indexOf(match[4]) === 0 || str.indexOf(match[5]) === 0){}; x += 1; }
indexOf start anonymous
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]; match.some(m => str.indexOf(m) === 0); x += 1; }
indexOf start named
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]; const check = (m) => str.indexOf(m) === 0; match.some(check); x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
regex start
startsWith
startsWith anonymous
indexOf start
indexOf start anonymous
indexOf start named
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):
Measuring the performance of different JavaScript regular expression and string manipulation methods is crucial for optimizing code efficiency. Let's break down the benchmark and its components. **Benchmark Overview** The provided benchmark compares four methods for searching strings that match certain patterns: 1. `regex.start`: A custom regular expression (regex) pattern. 2. `startsWith`: The `String.prototype.startsWith()` method. 3. `startsWith anonymous`: An anonymous function equivalent to `startsWith`. 4. `indexOf start` and its variants (`indexOf start named`): Using the `String.indexOf()` method with an index of 0. **Benchmark Preparation Code** The script preparation code defines: * A custom regex pattern `window.regexStart` for matching strings that contain specific characters. * An array of matches `window.match` containing six unique strings. * The length of the match array `window.matchLength`. * A data set `window.data` containing 100,000 random strings. **Individual Test Cases** Each test case defines a single iteration of the benchmark. Here's what each method does: 1. **regex.start**: Applies the custom regex pattern to each string in the data set. 2. **startsWith**: Uses the `String.prototype.startsWith()` method to check if each string starts with any of the match characters. 3. **startsWith anonymous**: Creates an anonymous function equivalent to `startsWith` and applies it to each string in the data set using `Array.prototype.some()`. 4. **indexOf start`: Uses the `String.indexOf()` method with an index of 0 to search for matches. **Benchmark Results** The latest benchmark results show the performance metrics for each test case on a Chrome 110 browser running on Linux: * **startsWith**: The fastest, with approximately 615 executions per second. * **regex.start**: Slower than `startsWith`, but still relatively fast (574 executions per second). * **startsWith anonymous**: Significantly slower than both `startsWith` and `regex.start` (321 executions per second). * **indexOf start** and its variants: The slowest, with execution rates around 146-129 executions per second. In summary: * `regex.start` is the fastest method for this specific pattern. * `startsWith` is faster than `regex.start`, but not as efficient. * Anonymous functions are less performant due to overhead. * `indexOf start` and its variants are the slowest methods, likely due to the use of a slower algorithm. Keep in mind that these results may vary depending on the specific use case and requirements.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf simpler
JS Regex vs .startsWith vs .indexOf (largerStrings)
JS Regex vs .startsWith vs .indexOf (string length 50 - 550)
JS Regex vs .startsWith vs .indexOf vs. slice equals vs substring
JS Regex vs .startsWith
Comments
Confirm delete:
Do you really want to delete benchmark?