String repeat (v4)

Revision 4 of this benchmark created on


Description

Comparing if it's faster to repeat a string using Array.join technique or a simple for loop.

Preparation HTML

<script>
  String.prototype.repeatA = function(count) {
      if (count < 1) return '';
      var result = '', pattern = this.valueOf();
      while (count > 0) {
          if (count & 1) result += pattern;
          count >>= 1, pattern += pattern;
      };
      return result;
  };
  
  
  String.prototype.repeatB = function(num) {
   var i = 1,
       result = this;
   for (; i < num; ++i)
   result += this;
   return result;
  }
  
  var s1 = s2 = "helo",
      r1 = r2 = "";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
using fastest
r1 = s1.repeatA(10000);
ready
Using simple for loop
r2 = s2.repeatB(10000);
ready

Revisions

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