Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better jQuery Pattern Append #101

Open
rizalp opened this issue Nov 13, 2013 · 1 comment
Open

Better jQuery Pattern Append #101

rizalp opened this issue Nov 13, 2013 · 1 comment

Comments

@rizalp
Copy link

rizalp commented Nov 13, 2013

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(''));
@oshanz
Copy link

oshanz commented May 5, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants