array join vs string connect

JavaScript performance comparison

Test case created by AlienWebguy

Preparation code

  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 = [];
  var str_to_split = alphatmp.join(',');
  var myarray = str_to_split.split(',');

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
array join
var tmp = [];
for (i=0;i<myarray.length;i++){

var output = tmp.join('');
string connect
var output = '';
for (i=0;i<myarray.length;i++){
output += myarray[i];

Compare results of other browsers


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


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 )

moontaebu commented :

Testing in Chrome 38.0.2125.104 on Windows Server 2008 R2 / 7

The results were:

array join: 45,120 | ±6.73% | 77% slower

string connect: 184,143 ±2.02% fastest

etetet commented :