padLeft

JavaScript performance comparison

Test case created

Info

Two ways to pad a string

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
        function padLeftLoop(numString) {
                for (var l = 8 - numString.length; l > 0; l--) {
                        numString = "0" + numString;
                }
                return numString;
        }
   
        function padLeftLoopWhile(numString) {
        var len = 8 - numString.length;
                while (len--) {
                        numString = "0" + numString;
                }
                return numString;
        }
   
    function padLeftAppendTrim(numString) {
      numString = "00000000" + numString;
      return numString.replace(/.*(.{8})$/, "$2");
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Loop
padLeftLoop("123");
 
pending…
Pad and Trim
padLeftAppendTrim("123");
pending…
While loop
padLeftLoopWhile("123");
pending…

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

Compare results of other browsers

0 comments

Add a comment