String repeat

JavaScript performance comparison

Revision 2 of this test case created


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

Preparation code

  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 = "";

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);
Using simple for loop
r2 = s2.repeatB(10000);

Compare results of other browsers


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:


Comment form temporarily disabled.

Add a comment