<div></div>
window.regexStart = /^test/;
window.regexEnd = /test$/;
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)));
}
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;
}
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;
}
var x = 0;
var TOTAL_STRINGS = window.TOTAL_STRINGS;
var data = window.data;
var regex = window.regexEnd;
while (x < TOTAL_STRINGS) {
const str = data[x];
regex.test(str);
x += 1;
}
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;
}
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;
}
var x = 0;
var TOTAL_STRINGS = window.TOTAL_STRINGS;
var data = window.data;
var match = window.match;
var length = window.matchLength;
while (x < TOTAL_STRINGS) {
const str = data[x];
str.indexOf(match) === str.length - length;
x += 1;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
regex start | |
startsWith | |
regex end | |
endsWith | |
indexOf start | |
indexOf end |
Test name | Executions per second |
---|---|
regex start | 569.8 Ops/sec |
startsWith | 1370.3 Ops/sec |
regex end | 289.5 Ops/sec |
endsWith | 860.9 Ops/sec |
indexOf start | 10606.7 Ops/sec |
indexOf end | 11877.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
The provided benchmark is designed to compare the performance of different string manipulation methods: Regular Expressions (Regex), startsWith
, and indexOf
. The test cases cover various scenarios, including searching for a specific substring at the start or end of a string.
Benchmark Definition
The benchmark definition consists of two parts:
window.regexStart
and window.regexEnd
: Regular expression patterns to match at the start and end of strings, respectively.window.match
and window.matchLength
: A string to search for and its length, respectively.window.data
: An array of random strings generated using a custom function.TOTAL_STRINGS
: The total number of strings in the data array.Individual Test Cases
There are six test cases, each with its own benchmark definition:
startsWith
method to check if a string starts with a specific substring.endsWith
method to check if a string ends with a specific substring.Benchmark Results
The benchmark results show the performance of each test case across multiple executions per second (EPS) on a Chrome 129 browser running on a Mac OS X 10.15.7 system. The results are as follows:
TestName | ExecutionsPerSecond |
---|---|
indexOf end | 20028.25390625 |
indexOf start | 19962.216796875 |
endsWith | 1907.3641357421875 |
startsWith | 1848.285888671875 |
regex start | 871.0967407226562 |
regex end | 765.4481201171875 |
Analysis
The results show that:
indexOf
is the fastest method for searching at both the start and end of a string.startsWith
and endsWith
are slower than indexOf
, but still relatively fast.In general, these results suggest that if you need to perform frequent string searches, using indexOf
might be a better choice. However, if you need more control over the search pattern, Regular Expressions (Regex) can still be a viable option.