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 () {
var isDigit = /\d/;
var toNumber = Number;
var pow = Math.pow;
String.prototype.format1 = function () {
// todo: Throw an exception when we don't support the type of the argument
var args = [];
for (var _i = 0; _i < (arguments.length - 0); _i++) {
args[_i] = arguments[_i + 0];
}
var input = this;
var inputLength = input.length;
var output = "";
var token = "";
for (var i = 0; i < inputLength; i++) {
var ch = input[i];
var tokenLength = token.length;
switch (ch) {
case "{":
if (tokenLength > 0) {
output += token;
}
token = ch;
break;
case "}":
if (tokenLength > 0) {
var index = 0;
var length = tokenLength - 1;
for (var n = 0; n < length; n++) {
index += pow(10, n) * toNumber(token[length - n]);
}
// todo: Throw exception if the index is greater than the length of the argument list
output += args[index];
token = "";
} else {
output += ch;
}
break;
default: {
if (tokenLength > 0) {
if (isDigit.test(ch)) {
token += ch;
} else {
output += token + ch;
token = "";
}
} else {
output += ch;
}
}
}
}
return output;
};
})();
(function () {
String.prototype.format2 = function () {
// todo: Throw an exception when we don't support the type of the argument
var args = [];
for (var _i = 0; _i < (arguments.length - 0); _i++) {
args[_i] = arguments[_i + 0];
}
var input = this;
var inputLength = input.length;
var output = "";
var token = "";
var isDigit = /\d/;
var toNumber = Number;
var pow = Math.pow;
for (var i = 0; i < inputLength; i++) {
var ch = input[i];
var tokenLength = token.length;
switch (ch) {
case "{":
if (tokenLength > 0) {
output += token;
}
token = ch;
break;
case "}":
if (tokenLength > 0) {
var index = 0;
var length = tokenLength - 1;
for (var n = 0; n < length; n++) {
index += pow(10, n) * toNumber(token[length - n]);
}
// todo: Throw exception if the index is greater than the length of the argument list
output += args[index];
token = "";
} else {
output += ch;
}
break;
default: {
if (tokenLength > 0) {
if (isDigit.test(ch)) {
token += ch;
} else {
output += token + ch;
token = "";
}
} else {
output += ch;
}
}
}
}
return output;
};
})();
Ready to run.
Test | Ops/sec | |
---|---|---|
format1 |
| ready |
format2 |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.