String concatenation (v84)

Revision 84 of this benchmark created on


Description

Different ways to concatenate strings together

Preparation HTML

<script>
  var str1 = "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefga";

  var str2 = "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";

  var str3 = "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";

  var str4 = "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";

  var arr = [];
  
for(var i = 0; i<100; i++){
  arr.push("abcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefg");
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Direct concatenation
var foo = arr[0] + arr[1] + arr[2] + arr[3] + arr[4] + arr[5] + arr[6] + arr[7] + arr[8] + arr[9] + arr[10] + arr[11] + arr[12] + arr[13] + arr[14] + arr[15] + arr[16] + arr[17] + arr[18] + arr[19] + arr[20] + arr[21] + arr[22] + arr[23] + arr[24] + arr[25] + arr[26] + arr[27] + arr[28] + arr[29] + arr[30] + arr[31] + arr[32] + arr[33] + arr[34] + arr[35] + arr[36] + arr[37] + arr[38] + arr[39] + arr[40] + arr[41] + arr[42] + arr[43] + arr[44] + arr[45] + arr[46] + arr[47] + arr[48] + arr[49] + arr[50] + arr[51] + arr[52] + arr[53] + arr[54] + arr[55] + arr[56] + arr[57] + arr[58] + arr[59] + arr[60] + arr[61] + arr[62] + arr[63] + arr[64] + arr[65] + arr[66] + arr[67] + arr[68] + arr[69] + arr[70] + arr[71] + arr[72] + arr[73] + arr[74] + arr[75] + arr[76] + arr[77] + arr[78] + arr[79] + arr[80] + arr[81] + arr[82] + arr[83] + arr[84] + arr[85] + arr[86] + arr[87] + arr[88] + arr[89] + arr[90] + arr[91] + arr[92] + arr[93] + arr[94] + arr[95] + arr[96] + arr[97] + arr[98] + arr[99];
ready
Individual += statements
var foo = str1;
foo += arr[0];
foo += arr[1];
foo += arr[2];
foo += arr[3];
foo += arr[4];
foo += arr[5];
foo += arr[6];
foo += arr[7];
foo += arr[8];
foo += arr[9];
foo += arr[10];
foo += arr[11];
foo += arr[12];
foo += arr[13];
foo += arr[14];
foo += arr[15];
foo += arr[16];
foo += arr[17];
foo += arr[18];
foo += arr[19];
foo += arr[20];
foo += arr[21];
foo += arr[22];
foo += arr[23];
foo += arr[24];
foo += arr[25];
foo += arr[26];
foo += arr[27];
foo += arr[28];
foo += arr[29];
foo += arr[30];
foo += arr[31];
foo += arr[32];
foo += arr[33];
foo += arr[34];
foo += arr[35];
foo += arr[36];
foo += arr[37];
foo += arr[38];
foo += arr[39];
foo += arr[40];
foo += arr[41];
foo += arr[42];
foo += arr[43];
foo += arr[44];
foo += arr[45];
foo += arr[46];
foo += arr[47];
foo += arr[48];
foo += arr[49];
foo += arr[50];
foo += arr[51];
foo += arr[52];
foo += arr[53];
foo += arr[54];
foo += arr[55];
foo += arr[56];
foo += arr[57];
foo += arr[58];
foo += arr[59];
foo += arr[60];
foo += arr[61];
foo += arr[62];
foo += arr[63];
foo += arr[64];
foo += arr[65];
foo += arr[66];
foo += arr[67];
foo += arr[68];
foo += arr[69];
foo += arr[70];
foo += arr[71];
foo += arr[72];
foo += arr[73];
foo += arr[74];
foo += arr[75];
foo += arr[76];
foo += arr[77];
foo += arr[78];
foo += arr[79];
foo += arr[80];
foo += arr[81];
foo += arr[82];
foo += arr[83];
foo += arr[84];
foo += arr[85];
foo += arr[86];
foo += arr[87];
foo += arr[88];
foo += arr[89];
foo += arr[90];
foo += arr[91];
foo += arr[92];
foo += arr[93];
foo += arr[94];
foo += arr[95];
foo += arr[96];
foo += arr[97];
foo += arr[98];
foo += arr[99];
ready
Individual statements
var foo = str1;
foo = foo + arr[0];
foo = foo + arr[1];
foo = foo + arr[2];
foo = foo + arr[3];
foo = foo + arr[4];
foo = foo + arr[5];
foo = foo + arr[6];
foo = foo + arr[7];
foo = foo + arr[8];
foo = foo + arr[9];
foo = foo + arr[10];
foo = foo + arr[11];
foo = foo + arr[12];
foo = foo + arr[13];
foo = foo + arr[14];
foo = foo + arr[15];
foo = foo + arr[16];
foo = foo + arr[17];
foo = foo + arr[18];
foo = foo + arr[19];
foo = foo + arr[20];
foo = foo + arr[21];
foo = foo + arr[22];
foo = foo + arr[23];
foo = foo + arr[24];
foo = foo + arr[25];
foo = foo + arr[26];
foo = foo + arr[27];
foo = foo + arr[28];
foo = foo + arr[29];
foo = foo + arr[30];
foo = foo + arr[31];
foo = foo + arr[32];
foo = foo + arr[33];
foo = foo + arr[34];
foo = foo + arr[35];
foo = foo + arr[36];
foo = foo + arr[37];
foo = foo + arr[38];
foo = foo + arr[39];
foo = foo + arr[40];
foo = foo + arr[41];
foo = foo + arr[42];
foo = foo + arr[43];
foo = foo + arr[44];
foo = foo + arr[45];
foo = foo + arr[46];
foo = foo + arr[47];
foo = foo + arr[48];
foo = foo + arr[49];
foo = foo + arr[50];
foo = foo + arr[51];
foo = foo + arr[52];
foo = foo + arr[53];
foo = foo + arr[54];
foo = foo + arr[55];
foo = foo + arr[56];
foo = foo + arr[57];
foo = foo + arr[58];
foo = foo + arr[59];
foo = foo + arr[60];
foo = foo + arr[61];
foo = foo + arr[62];
foo = foo + arr[63];
foo = foo + arr[64];
foo = foo + arr[65];
foo = foo + arr[66];
foo = foo + arr[67];
foo = foo + arr[68];
foo = foo + arr[69];
foo = foo + arr[70];
foo = foo + arr[71];
foo = foo + arr[72];
foo = foo + arr[73];
foo = foo + arr[74];
foo = foo + arr[75];
foo = foo + arr[76];
foo = foo + arr[77];
foo = foo + arr[78];
foo = foo + arr[79];
foo = foo + arr[80];
foo = foo + arr[81];
foo = foo + arr[82];
foo = foo + arr[83];
foo = foo + arr[84];
foo = foo + arr[85];
foo = foo + arr[86];
foo = foo + arr[87];
foo = foo + arr[88];
foo = foo + arr[89];
foo = foo + arr[90];
foo = foo + arr[91];
foo = foo + arr[92];
foo = foo + arr[93];
foo = foo + arr[94];
foo = foo + arr[95];
foo = foo + arr[96];
foo = foo + arr[97];
foo = foo + arr[98];
foo = foo + arr[99];
ready
Using Array#join
var foo = arr.join('');
ready
Single individual statement
var foo = str1;
foo += str2 + str3 + str4;
ready
Function based (for)
function concat(arr) {
  len = arr.length;
  for (s = "", i = 0; i < len; s += arr[i], i++);
  return s;
}

var foo = concat(arr);
ready
concat
var foo = str1;
foo = foo.concat(str2);
foo = foo.concat(str3);
foo = foo.concat(str4);
ready
concat once
var foo = str1.concat(str2, str3, str4)
ready

Revisions

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