Number Padding (v14)

Revision 14 of this benchmark created by will on


Description

Find the most efficient way to pad a number

Test runner

Ready to run.

Testing in
TestOps/sec
Lazy Man Way
(function(myInteger, paddingLength) {
 myInteger = myInteger.toString();

 var myIntegerLength = myInteger.length,
     paddingLength = paddingLength || 5,
     loopIterations = paddingLength - myIntegerLength,
     padding = "";

 if (paddingLength > myIntegerLength) {
  for (var i = 0; i < loopIterations; i++) {
   padding = ["0", padding].join('');
  }
 }

 return [padding, myInteger].join('');
})(50000, 22);
ready
Reverse While Loop w / Array + slice()
(function(myInteger, paddingLength) {

 var x = [];

 while (paddingLength--) {
  x.push(0);
 }

 x.push(myInteger);
 return x.slice(-(paddingLength - ("" + myInteger).length));

})(50000, 22);
ready
Reverse While Loop w / Strignified Array + slice()
(function(myInteger, paddingLength) {

 var x = [];

 while (paddingLength--) {
  x.push(0);
 }

 x.push(myInteger);
 x = x.join("");

 return x.slice(-(paddingLength - ("" + myInteger).length));

})(50000, 22);
ready
Reverse While Loop w / String + slice()
(function(myInteger, paddingLength) {

 var x = "";

 while (paddingLength--) {
  x += 0;
 }

 x += myInteger;

 return x.slice(-(paddingLength - ("" + myInteger).length));

})(50000, 22);
ready
Reverse While Loop w / Array length + slice()
(function(myInteger, paddingLength) {

 var x = [];

 while (paddingLength--) {
  x[x.length] = 0;
 }

 x[x.length] = myInteger;

 return x.slice(-(paddingLength - ("" + myInteger).length)).join("");

})(50000, 22);
ready
A Little Algorithm I devised
(function(myInteger, paddingLength) {

 var intString = (myInteger + ""),
     intLength = intString.length,
     x = intString;

 if (paddingLength > intLength) {
  if (paddingLength <= 20) {
   x = ((Math.pow(10, paddingLength) + "").substr(1) + intString).slice(-paddingLength);
  }
  else {
   var x = (Math.pow(10, 10) + "").substr(1),
       s = "";
   z = parseInt(paddingLength / 10);

   while (z--) {
    s += x;
   }

   r = paddingLength % 10;
   s += (Math.pow(10, r) + "").substr(1)

   x = (s + intString).slice(-paddingLength)
  }
 }

 return x;

})(50000, 22);
ready
A Little improved Algorithm I devised
(function(myInteger, paddingLength) {

 var intString = (myInteger + ""),
     intLength = intString.length,
     x = intString;

 if (paddingLength > intLength) {
  if (paddingLength <= 20) {
   x = ((Math.pow(10, paddingLength) + "").substr(1) + intString).slice(-paddingLength);
  }
  else {
   var x = (Math.pow(10, 10) + "").substr(1),
       s = "";
   z = paddingLength / 10 >> 0;

   while (z--) {
    s += x;
   }

   r = paddingLength % 10;
   s += (Math.pow(10, r) + "").substr(1)

   x = (s + intString).slice(-paddingLength)
  }
 }

 return x;

})(50000, 22);
ready
Another slight improvement
(function(myInteger, paddingLength) {

 var intString = (myInteger + ""),
     intLength = intString.length,
     x = intString;

 if (paddingLength > intLength) {
  if (paddingLength <= 20) {
   x = ((Math.pow(10, paddingLength) + "").substr(1) + intString).slice(-paddingLength);
  }
  else {
   var x = (Math.pow(10, 10) + "").substr(1),
       s = "";
   z = paddingLength / 10 >> 0;

   while (z--) {
    s = s + x;
   }

   r = paddingLength % 10;
   s = s + (Math.pow(10, r) + "").substr(1)

   x = (s + intString).slice(-paddingLength)
  }
 }

 return x;

})(50000, 22);
ready
A slightly little bit more improved
(function(myInteger, paddingLength) {

 var intString = myInteger >> 0,
     intLength = intString % 10,
     x = intString,
     s = "",
     z;

 if (paddingLength > intLength) {
  if (paddingLength <= 20) 
   x = ((Math.pow(10, paddingLength) + "").substr(1) + intString).slice(-paddingLength);
  else {
   x = (Math.pow(10, 10) + "").substr(1);
   z = paddingLength / 10 >> 0;

   while (z--) 
    s = s + x;

   r = paddingLength % 10;
   s = s + (Math.pow(10, r) + "").substr(1)

   x = (s + intString).slice(-paddingLength)
  }
 }

 return x;

})(50000, 22);
ready
Yet another attempt at a squeeze
(function(myInteger, paddingLength) {

 var intString = myInteger >> 0,
     intLength = intString % 10,
     x = intString,
     s = "",
     z;

 if (paddingLength > intLength) {
  if (paddingLength <= 20) 
   x = (String(Math.pow(10, paddingLength)).substr(1) + intString).slice(-paddingLength);
  else {
   x = String(Math.pow(10, 10)).substr(1);
   z = paddingLength / 10 >> 0;

   while (z--) 
    s = s + x;

   r = paddingLength % 10;
   s = s + (String(Math.pow(10, r))).substr(1);

   x = (s + intString).slice(-paddingLength);
  }
 }

 return x;

})(50000, 22);
ready
Roman Style
(function(myInteger, paddingLength) {

  var padding = ['',
                 '0',
                 '00',
                 '000',
                 '0000',
                 '00000',
                 '000000',
                 '0000000',
                 '00000000',
                 '000000000',
                 '0000000000'
                ],
  
      exponent = (paddingLength === 0)
                   ? 0 
                   : Math.pow(10, parseInt(Math.log(paddingLength) / Math.log(10))), 
      multipliers, output, x,str = ("" + myInteger);

  paddingLength -= str.length;
 
  if (paddingLength < 0)
   {
    output = str;
   }
  else if (paddingLength < 10 && paddingLength > -1)
   {
    output = padding[paddingLength] + myInteger;
   }
  else
   {    
    x = paddingLength / 10;
    multipliers = parseInt(x);
    
    output = (new Array(multipliers + 1).join(padding[10])) + padding[x * 10 % 10] + myInteger;
   }

 return output;
})(50000, 22);
ready
O(ln n)
(function(myInteger, paddingLength) {
    var intString = myInteger+ "",
        numZeroes = paddingLength - intString.length,
        totalPadding = "",
        z = 1;

    while(totalPadding.length < numZeroes){
        var padding = "0";
        while((z = z << 1) && z < paddingLength){
            padding += padding;
        }
        totalPadding += padding;
    }
    return totalPadding + intString;
})(50000, 220);
ready

Revisions

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

  • Revision 1: published by Milan Adamovsky on
  • Revision 2: published by Milan Adamovsky on
  • Revision 3: published by Milan Adamovsky on
  • Revision 5: published by Milan Adamovsky on
  • Revision 6: published by Larry LeFever on
  • Revision 7: published by JP Grace on
  • Revision 8: published on
  • Revision 9: published by Larry LeFever on
  • Revision 11: published by Milan Adamovsky on
  • Revision 12: published by Milan Adamovsky on
  • Revision 13: published by Milan Adamovsky on
  • Revision 14: published by will on
  • Revision 15: published on
  • Revision 17: published by Phil Tang on