<div id=''></div>
window.target = '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)));
}
var regex = new RegExp('^'+window.target,'i');
window.data.forEach(str => regex.test(str));
window.data.forEach(str => str.toLowerCase().startsWith(window.target.toLowerCase()));
window.data.forEach(str => str.toLowerCase().indexOf(window.target.toLowerCase()) === 0);
window.data.forEach(str => {
str.subStr(0,window.target.length).localeCompare(window.target, undefined, { sensitivity: 'base', usage:'search'});
});
window.data.forEach(str => {
(new RegExp('^'+window.target,'i')).test(str);
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Regex outside loop | |
startsWith() | |
indexOf() | |
localeCompare() | |
Regex inside loop |
Test name | Executions per second |
---|---|
Regex outside loop | 396.6 Ops/sec |
startsWith() | 49.2 Ops/sec |
indexOf() | 44.0 Ops/sec |
localeCompare() | 0.0 Ops/sec |
Regex inside loop | 22.3 Ops/sec |
Measuring performance differences between various JavaScript string comparison methods can be crucial for optimizing code and ensuring robustness.
Benchmark Overview
The provided benchmark compares the execution times of four string comparison methods:
new RegExp('^'+window.target,'i')
with the test()
method.startsWith()
method on a lowercase version of the input string (str.toLowerCase().startsWith(window.target.toLowerCase())
).indexOf()
method with an offset of 0 to match the start of the string (str.toLowerCase().indexOf(window.target.toLowerCase()) === 0
).localeCompare()
method on a substring of the input string (str.subStr(0,window.target.length).localeCompare(window.target, undefined, { sensitivity: 'base', usage:'search' })
).Comparison Options
The options compared are:
startsWith()
and indexOf()
methods.Pros and Cons
startsWith()
, this method has an optimized implementation but might not work correctly for all scenarios, especially when dealing with non-ASCII characters or Unicode strings.Special Considerations
The benchmark uses the window.target = 'tEsT';
line, which sets a special variable target
containing the input string 'tEsT'
. This is done to isolate the test from other variables and focus solely on the comparison methods.
Additionally, the benchmark generates random strings using the getRandomInt()
and makeRandomString()
functions. This helps ensure that the results are representative of real-world scenarios where string data is generated dynamically.
Alternatives
Other alternatives for string comparison in JavaScript include:
URL.createObjectURL()
and Blob
objects, to handle binary data.The provided benchmark provides a solid foundation for comparing the performance of different string comparison methods in JavaScript. It can be extended or modified to explore other scenarios and optimizations.