String repeat

JavaScript performance comparison

Revision 2 of this test case created

Info

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

Preparation code

<script>
  String.prototype.repeatA = function(num) {
   return new Array(num + 1).join(this);
  }
 
  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

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
using Array.join
r1 = s1.repeatA(10000);
pending…
Using simple for loop
r2 = s2.repeatB(10000);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment