String Concatenation Performance Test
Compare the string concatenation speed between using + or array.join().
by hyjk2000
JavaScript
/* String Concatenation Performance Test
* (C)2011 James Shi
* http://hyjk2000.sinaapp.com/?p=405
*/
function fnPlus() {
var str = '', i = 1000000;
while (i--) str += 'thequickbrownfoxjumpsoverthelazydog195476825412355849751364859';
}
function fnArray() {
var buffer = [], i = 1000000;
while (i--) buffer[i] = 'thequickbrownfoxjumpsoverthelazydog195476825412355849751364859';
var str = buffer.join('');
}
// Concat using + operator, will freeze IE6
var time_start = (new Date()).getTime();
fnPlus();
var time_done = (new Date()).getTime();
document.writeln('<p>Concat using + operator: <b>' + (time_done - time_start) + '</b> ms</p>');
// Concat using Array.join()
time_start = (new Date()).getTime();
fnArray();
time_done = (new Date()).getTime();
document.writeln('<p>Concat using Array.join(): <b>' + (time_done - time_start) + '</b> ms</p>');