jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
function findFirst(string, regex, startpos) {
var indexOf = string.substring(startpos || 0).search(regex);
return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf;
}
function reverseString(str) {
var newString = "";
for (var i = str.length - 1; i >= 0; i--) {
newString += str[i];
}
return newString;
}
function findAllReturnLast(string, regex, startpos) {
regex = (regex.global) ? regex : new RegExp(regex.source, "g" + (regex.ignoreCase ? "i" : "") + (regex.multiLine ? "m" : ""));
if(typeof (startpos) == "undefined") {
startpos = string.length;
} else if(startpos < 0) {
startpos = 0;
}
var stringToWorkWith = string.substring(0, startpos + 1);
var lastIndexOf = -1;
var nextStop = 0;
var result;
while((result = regex.exec(stringToWorkWith)) != null) {
lastIndexOf = result.index;
regex.lastIndex = ++nextStop;
}
return lastIndexOf;
}
function reverseTextAndFindFirst(string, regex, startpos = 0) {
return string.length - findFirst(reverseString(string), regex, startpos) - 1;
}
const testString1 = "foo-123-bar-456"
const testString2 = "foo-123"
const testString3 = "---------------"
const testString4 = "foo123bar456"
const regex = /[^\w\d]/
Ready to run.
Test | Ops/sec | |
---|---|---|
find all and return the last match |
| ready |
reverse text and find the first match |
| ready |
find the last match using negative lookahead |
| ready |
find the first match (this just for a comparison with a native solution) |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.