Left zero filling

JavaScript performance comparison

Revision 6 of this test case created by vamp

Preparation code

<script>
var pad = 20;
var n = 3000;
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var s = String(n);

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Array.join
new Array(pad - s.length + 1).join('0') + s;
pending…
toFixed
(s / (Math.pow(10, pad))).toFixed(pad).substring(2);
pending…
while loop
while (s.length < pad) {
    s = '0' + s;
}

pending…
for loop
for(;s.length < pad; s='0'+s);
pending…
for loop2
for(var i=pad;--i; s='0'+s);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments