padding with memoization

JavaScript performance comparison

Test case created

Info

Comparing the performance of storing the string in a temporary variable.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var input = [0, "000", 1, 5, 99, 100, 200.23, 789, 2000, "2912", 12345, "88888", 999999];
   
    function runner(func) {
      for (var i = 0; i < input.length; i++) {
        for (var l = 0; l < 10; l++) {
          func(input[i], '0', l);
        }
      }
    }
};
</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
repeated - original
runner(function (toPad, padChar, length) {
  return (String(toPad).length < length) ? new Array(length - String(toPad).length + 1).join(padChar) + String(toPad) : toPad;
});
 
pending…
memoization - original
runner(function (toPad, padChar, length) {
  var s = String(toPad);
  return (s.length < length) ? new Array(length - s.length + 1).join(padChar) + s : toPad;
});
pending…
repeat - no news
runner(function (toPad, padChar, length) {
  return (String(toPad).length < length) ? Array(length - String(toPad).length + 1).join(padChar) + String(toPad) : toPad;
});
pending…
memoization - no news
runner(function (toPad, padChar, length) {
  var s = String(toPad);
  return (s.length < length) ? Array(length - s.length + 1).join(padChar) + s : toPad;
});
pending…
repeat - new
runner(function (toPad, padChar, length) {
  return (new String(toPad).length < length) ? new Array(length - new String(toPad).length + 1).join(padChar) + new String(toPad) : toPad;
});
pending…
memoization - new
runner(function (toPad, padChar, length) {
  var s = new String(toPad);
  return (s.length < length) ? new Array(length - s.length + 1).join(padChar) + s : toPad;
});
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