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 () {
String.prototype.format1 = function () {
// todo: Throw exception when we don't support the type of the argument
// todo: Throw exception when this is not a string
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 = input;
var token = "";
for (var i = 0; i < inputLength; i++) {
var ch = input[i];
var tokenLength = token.length;
switch (ch) {
case "{":
token = ch;
break;
case "}":
var value = "";
if (tokenLength > 0) {
token += ch;
var index = token.match(/\{(\d+)\}/);
if (index != null && index.length === 2) {
// todo: Throw exception if the index is greater than the length of the argument list
value = args[index[1]];
}
}
output = output.replace(token, value);
token = "";
break;
default: {
if (tokenLength > 0) {
token += ch;
}
}
}
}
return output;
};
})();
(function () {
var isDigit = /\d/;
var toNumber = Number;
var pow = Math.pow;
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 = "";
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.format3 = function (args) {
var str = this;
return str.replace(String.prototype.format3.regex, function(item) {
var intVal = parseInt(item.substring(1, item.length - 1));
var replace;
if (intVal >= 0) {
replace = args[intVal];
} else if (intVal === -1) {
replace = "{";
} else if (intVal === -2) {
replace = "}";
} else {
replace = "";
}
return replace;
});
};
String.prototype.format3.regex = new RegExp("{-?[0-9]+}", "g");
})();
(function () {
String.prototype.format4 = function() {
var formatted = this;
for (var i = 0; i < arguments.length; i++) {
var regexp = new RegExp('\\{'+i+'\\}', 'gi');
formatted = formatted.replace(regexp, arguments[i]);
}
return formatted;
};
})();
(function () {
String.prototype.format5 = function() {
var str = this.toString();
if (!arguments.length)
return str;
var args = typeof arguments[0],
args = (("string" == args || "number" == args) ? arguments : arguments[0]);
for (arg in args)
str = str.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]);
return str;
};
})();
(function () {
var isDigit = /\d/;
var toNumber = Number;
var pow = Math.pow;
String.prototype.format6 = function () {
// todo: Throw exception when we don't support the type of the argument
// todo: Throw exception when this is not a string
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 = input;
var token = "";
for (var i = 0; i < inputLength; i++) {
var ch = input[i];
var tokenLength = token.length;
switch (ch) {
case "{":
token = ch;
break;
case "}":
var value = "";
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
value = args[index];
token += ch;
}
output = output.replace(token, value);
token = "";
break;
default: {
if (tokenLength > 0) {
if (isDigit.test(ch)) {
token += ch;
} else {
token = "";
}
}
}
}
}
return output;
};
})();
(function () {
String.prototype.format7 = function() {
var formatted = this;
for (var i = 0; i < arguments.length; i++) {
formatted = formatted.replace("{" + i + "}", arguments[i]);
}
return formatted;
};
})();
(function () {
String.prototype.format8 = function (pattern) {
// todo: Throw exception when we don't support the type of the argument
// todo: Throw exception when this is not a string
var args = [];
for (var _i = 0; _i < (arguments.length - 1); _i++) {
args[_i] = arguments[_i + 1];
}
var input = pattern;
var inputLength = input.length;
var output = input;
var token = "";
for (var i = 0; i < inputLength; i++) {
var ch = input[i];
var tokenLength = token.length;
switch (ch) {
case "{":
token = ch;
break;
case "}":
var value = "";
if (tokenLength > 0) {
token += ch;
var index = token.match(/\{(\d+)\}/);
if (index != null && index.length === 2) {
// todo: Throw exception if the index is greater than the length of the argument list
value = args[index[1]];
}
}
output = output.replace(token, value);
token = "";
break;
default: {
if (tokenLength > 0) {
token += ch;
}
}
}
}
return output;
};
})();
(function () {
String.prototype.format9 = function () {
// todo: Throw exception when we don't support the type of the argument
// todo: Throw exception when this is not a string
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 = input;
var token = "";
for (var i = 0; i < inputLength; i++) {
var ch = input.charAt(i);
var tokenLength = token.length;
switch (ch) {
case "{":
token = ch;
break;
case "}":
var value = "";
if (tokenLength > 0) {
token += ch;
var index = token.match(/\{(\d+)\}/);
if (index != null && index.length === 2) {
// todo: Throw exception if the index is greater than the length of the argument list
value = args[index[1]];
}
}
output = output.replace(token, value);
token = "";
break;
default: {
if (tokenLength > 0) {
token = token.concat(ch);
}
}
}
}
return output;
};
})();
(function () {
String.prototype.format10 = function () {
// todo: Throw exception when we don't support the type of the argument
// todo: Throw exception when this is not a string
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 = input;
var token = "";
for (var i = 0; i < inputLength; i++) {
var ch = input.charAt(i);
var tokenLength = token.length;
switch (ch) {
case "{":
token = ch;
break;
case "}":
var value = "";
if (tokenLength > 0) {
token += ch;
var index = token.match(/\{(\d+)\}/);
if (index != null && index.length === 2) {
// todo: Throw exception if the index is greater than the length of the argument list
value = args[index[1]];
}
}
output = output.replace(token, value);
token = "";
break;
default: {
if (tokenLength > 0) {
token += ch;
}
}
}
}
return output;
};
})();
(function () {
String.prototype.format11 = function () {
// todo: Throw exception when we don't support the type of the argument
// todo: Throw exception when this is not a string
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 = this;
var token = "";
var tokenLength;
var ch;
for (var i = 0; i < inputLength; i++) {
ch = input.charAt(i);
tokenLength = token.length;
switch (ch) {
case "{":
token = ch;
break;
case "}":
var value = "";
if (tokenLength > 0) {
token = token.concat(ch);
var index = token.match(/\{(\d+)\}/);
if (index != null && index.length === 2) {
// todo: Throw exception if the index is greater than the length of the argument list
value = args[index[1]];
}
}
output = output.replace(token, value);
token = "";
break;
default: {
if (tokenLength > 0) {
token = token.concat(ch);
}
}
}
}
return output;
};
})();
(function () {
String.prototype.format12 = function () {
// todo: Throw exception when we don't support the type of the argument
var args = [];
for (var r = 0; r < arguments.length; r++) {
args[r] = arguments[r];
}
// todo: Throw exception when this is not a string
var input = this;
var inputLength = input.length;
var output = this;
var token = "";
var tokenLength;
var index = "";
var value;
var ch;
for (var i = 0; i < inputLength; i++) {
ch = input.charAt(i);
tokenLength = token.length;
switch (ch) {
case "{":
token = ch;
break;
case "}":
value = index = "";
if (tokenLength > 0) {
token = token.concat(ch);
for (var j = 1; j <= token.search(/\d\b/); j++) {
index = index.concat(token[j]);
}
// todo: Throw exception if the index is greater than the length of the argument list
if (index != "") {
value = args[index];
}
}
output = output.replace(token, value);
token = "";
break;
default: {
if (tokenLength > 0) {
token = token.concat(ch);
}
}
}
}
return output;
};
})();
(function () {
String.prototype.format13 = function () {
// todo: Throw exception when we don't support the type of the argument
var args = [];
for (var r = 0; r < arguments.length; r++) {
args[r] = arguments[r];
}
// todo: Throw exception when this is not a string
var input = this;
var inputLength = input.length;
var output = this;
var token = "";
var tokenLength;
var index;
var value;
var ch;
for (var i = 0; i < inputLength; i++) {
ch = input[i];
tokenLength = token.length;
switch (ch) {
case "{":
token = ch;
break;
case "}":
value = index = "";
if (tokenLength > 0) {
token += ch;
for (var j = 1; j <= token.search(/\d\b/); j++) {
index += token[j];
}
// todo: Throw exception if the index is greater than the length of the argument list
if (index != "") {
value = args[index];
}
}
output = output.replace(token, value);
token = "";
break;
default: {
if (tokenLength > 0) {
token += ch;
}
}
}
}
return output;
};
})();
(function () {
var regex = /\{(\d+)(?:\:(.))?\}/gi;
String.prototype.formatX = function () {
// todo: Throw exception when we don't support the type of the argument
var args = [];
for (var r = 0; r < arguments.length; r++) {
args[r] = arguments[r];
}
// todo: Throw exception when this is not a string
var input = this;
return input.replace(regex, function (match, index, format) {
var value = "";
// Convert the string to number;
index = index * 1;
// todo: Throw exception if the index is greater than the length of the argument list
if (index >= 0) {
value = args[index];
}
return value;
});
};
})();
(function () {
var utils = {};
var regex = /\{(\d+)(?:\:(.))?\}/gi;
utils.format = function (pattern) {
// todo: Throw exception when we don't support the type of the argument
var args = [];
for (var r = 0; r < (arguments.length - 1); r++) {
args[r] = arguments[r + 1];
}
// todo: Throw exception when this is not a string
return pattern.replace(regex, function (match, index, format) {
var value = "";
// Convert the string to number;
index = index * 1;
// todo: Throw exception if the index is greater than the length of the argument list
if (index >= 0) {
value = args[index];
}
return value;
});
};
window.utils = window.utils || utils;
})();
(function () {
var utils = {};
var regex = /\{(\d+)(?:\:(.))?\}/gi;
utils.format = function () {
var input = arguments[0];
// todo: Throw exception when we don't support the type of the argument
var args = [];
for (var r = 0; r < (arguments.length - 1); r++) {
args[r] = arguments[r + 1];
}
// todo: Throw exception when this is not a string
// todo: Use the format parameter to determine what kind of format we want to apply to the value
return input.replace(regex, function (match, index, format) {
var value = "";
// Converts the string to number;
index = index * 1;
// todo: Throw exception if the index is greater than the length of the argument list
if (index >= 0) {
value = args[index];
}
return value;
});
};
window.utils2 = window.utils2 || utils;
})();
(function () {
String.prototype.format14 = function () {
var format = utils2.format;
var args = [];
args[0] = this;
for (var r = 0; r < arguments.length; r++) {
args[r + 1] = arguments[r];
}
return format.apply(this, args);
};
})();
Ready to run.
Test | Ops/sec | |
---|---|---|
replace |
| ready |
format1 |
| ready |
format2 |
| ready |
format3 |
| ready |
format4 |
| ready |
format5 |
| ready |
format6 |
| ready |
format7 |
| ready |
format8 |
| ready |
format9 |
| ready |
format10 |
| ready |
format11 |
| ready |
format12 |
| ready |
format13 |
| ready |
formatX |
| ready |
utils.format |
| ready |
utils2.format |
| ready |
format14 |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.