chuanxshi / javascript-patterns

JavaScript Design Patterns

Home Page:http://shichuan.github.io/javascript-patterns

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Better jQuery Pattern Append

rizalp opened this issue · 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(''));

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