array join vs string connect

JavaScript performance comparison

Test case created by AlienWebguy

Preparation code

<script>
  var alphabet = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
  var alphatmp = [];
 
  for(i=0;i<20;i++)
      alphatmp.push(alphabet);
 
  var str_to_split = alphatmp.join(',');
  var myarray = str_to_split.split(',');
 
</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
array join
var tmp = [];
for (i=0;i<myarray.length;i++){
  tmp.push(myarray[i]);
}

var output = tmp.join('');
pending…
string connect
var output = '';
for (i=0;i<myarray.length;i++){
output += myarray[i];
}
 
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:

2 comments

Rodrigo commented :

I know it's a bit old, but here's my comment. I didn't understand how this test works. The "array join" code completed first, so I thought it was the faster one. (testing in Firefox 19.0 32-bit on Windows Server 2008 R2 / 7 64-bit).

The results were: array join: 46,132 | ±7.85% | 33% slower string connect: 84,193 | ±2.75% | fastest

Doesn't the tests run through all the values in the loop and count the times, or does it keep running for some specific time and counts how many times did it run?

Jonathan Moore commented :

I was surprised to discover that manual loop unrolling can give a big speed up in chrome. ( see my revision 35 )

Add a comment