javascript-patterns icon indicating copy to clipboard operation
javascript-patterns copied to clipboard

Better jQuery Pattern Append

Open rizalp opened this issue 11 years ago • 1 comments

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(''));

rizalp avatar Nov 13 '13 04:11 rizalp

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

oshanz avatar May 05 '14 14:05 oshanz