String repeat

Benchmark created on


Description

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

Preparation HTML

<script>
  String.prototype.repeatA = function(num) {
   return new Array(num + 1).join(this);
  }
  
  String.prototype.repeatB = function(num) {
   var i = 1,
       result = this;
   while (i < num)
   result += this;
   return result;
  }
  
  var s1 = s2 = "helo",
      r1 = r2 = "";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
using Array.join
r1 = s1.repeatA(1000);
ready
Using simple while loop
r2 = s2.repeatB(1000);
ready

Revisions

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