Ways to 0-pad a number

JavaScript performance comparison

Revision 38 of this test case created by

Preparation code

<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>
<script src="http://epeli.github.io/underscore.string/dist/underscore.string.min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  /**
   * Pad a number with leading zeros to "pad" places:
   *
   * @param number: The number to pad
   * @param pad: The maximum number of leading zeros
   */
  
  function padNumberMath(number, pad) {
    var N = Math.pow(10, pad);
    return number < N ? ("" + (N + number)).slice(1) : "" + number
  }
  
  function padNumberArray(n, len) {
    var n_str = n.toString();
    if (n_str.length >= len) {
      return (n_str);
    }
    return (new Array(len + 1).join('0') + n).slice(-len);
  }
  
  function padNumberLoop(number, length) {
    var my_string = '' + number;
    while (my_string.length < length) {
      my_string = '0' + my_string;
    }
    return my_string;
  }
  
  function padNumberForLoop(number, length) {
    var my_string = '' + number;
    for (var i=0,endi=length-my_string.length;i < endi;i++) {
      my_string = '0' + my_string;
    }
    return my_string;
  }
  
  // padStr is the zero-pad string, e.g.: "0000"
  
  function padNumberString(number, padStr) {
    var len = padStr.length;
    number = number.toString();
    return number.length >= len ? number : (padStr + number).slice(-len);
  }
  
  function padRec(str, ch, len) {
    return str.length >= len ? str : padRec(ch + str, ch, len);
  }
  
  function padSplitWhileUnshiftJoin(number, len) {
    var result = String(number).split('');
    while (result.length < len) {
      result.unshift(0);
    }
    return result.join('');
  }
  
  
  function padWithUnderscoreString(number, len) {
    return _.pad(number, len, '0');
  }
  
  function padWhile(num, count) {
      var pad = count - (num + '').length;
      while(--pad > -1) {
          num = '0' + num;
      }
      return num;
  };
  
  String.prototype.padLeft = function(ch, len){
      return this.length >= len ? this : (ch + this).padLeft(ch, len)
  }
  
  

};
</script>

Preparation code output

<script src="http://epeli.github.io/underscore.string/dist/underscore.string.min.js"></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
Math
padNumberMath(1301, 4);
padNumberMath(120, 4);
padNumberMath(14, 4);
padNumberMath(9, 4);
pending…
Array join
padNumberArray(1301, 4);
padNumberArray(120, 4);
padNumberArray(14, 4);
padNumberArray(9, 4);
pending…
Loop
padNumberLoop(1301, 4);
padNumberLoop(120, 4);
padNumberLoop(14, 4);
padNumberLoop(9, 4);
pending…
String Pad
padNumberString(1301, '0000');
padNumberString(120, '0000');
padNumberString(14, '0000');
padNumberString(9, '0000');
pending…
Recursive
padRec('1301', '0', 4);
padRec('120', '0', 4);
padRec('14', '0', 4);
padRec('9', '0', 4);
pending…
Split-While-Unshift-Join
padSplitWhileUnshiftJoin(1301, 4);
padSplitWhileUnshiftJoin(120, 4);
padSplitWhileUnshiftJoin(14, 4);
padSplitWhileUnshiftJoin(9, 4);
pending…
Pad with Underscore.string
padWithUnderscoreString(1301, 4);
padWithUnderscoreString(120, 4);
padWithUnderscoreString(14, 4);
padWithUnderscoreString(9, 4);
pending…
Pad while
padWhile(1301, 4);
padWhile(120, 4);
padWhile(14, 4);
padWhile(9, 4);
pending…
OO-Recursive
'1301'.padLeft('0',4)
'120'.padLeft('0',4)
'14'.padLeft('0',4)
'9'.padLeft('0',4)
pending…
For Loop
padNumberForLoop(1301, 4);
padNumberForLoop(120, 4);
padNumberForLoop(14, 4);
padNumberForLoop(9, 4);
pending…
substr
('0000'+1301).substr(-4);
('0000'+120).substr(-4);
('0000'+14).substr(-4);
('0000'+9).substr(-4);
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