Posts Tagged ‘Performance’

Splitting a long JavaScript String across multiple lines

Wednesday, November 12th, 2008

Whilst reading a comment by Mike DeBoer on Ajaxian about delimiting JavaScript strings with backslashes, I thought it’s worth mentioning it here as well, if only to remind myself.

String concatenation always creates a new string.

Thus, writing:

 
var str = "This is a long" +
  " string. That's why we want to " +
  " split it over several lines";
 

is slower & more memory intensive than writing:

 
var str = "This is a long\
 string. That's why we want to\
 split it over several lines";
 

Just be careful you don’t have trailing spaces as that will mess it up.