Better jQuery Pattern Append
rizalp opened this issue · comments
Mohammad Shahrizal Prabowo commented
On https://github.com/shichuan/javascript-patterns/blob/master/jquery-patterns/append.html there's a slight consistency error with your other patterns. It is recomended to use array.push() and then join it instead of appending string over and over
So, instead of
var myhtml = '';
$.each(reallyLongArray, function (count, item) {
myhtml += '<li>' + item + '</li>';
});
$('#ballers').html(myhtml);
Should be
var myHtml = [];
$.each(reallyLongArray, function(count, item) {
myHtml.push('<li>' + item + '</li>');
});
$('#ballers').html(myHtml.join(''));
Oshan Wisumperuma commented
Yes it should change, Also we can improve it more because still you're using string concatenation.
try this ->
var myhtml = [];
$.each(reallyLongArray, function (count, item) {
myhtml.push('<li>',item,'</li>'); // now better
});
$('#ballers').html(myhtml.join(''));
reference ->
https://developers.google.com/speed/articles/optimizing-javascript