String formatting
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var text = 'Hello, my name is {0} {0} {1}!',
result;
String.prototype.format = function () {
var string = this;
for (var i = 0; i < arguments.length; i++) {
string = string.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
}
return string;
};
String.prototype.format2 = function () {
var string = this,
i = 0,
args = arguments.length;
for (; i < args; i++) {
string = string.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
}
return string;
};
String.prototype.format3 = function () {
var string = this,
args = arguments.length;
while (args--) {
string = string.replace(new RegExp('\\{' + args + '\\}', 'gm'), arguments[args]);
}
return string;
};
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
for |
|
pending… |
for2 |
|
pending… |
while |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments