JavaScript Benefits of `array.join` over string concatination while manipulating jQuery elements?

According to this

The syntax for adding new elements to the page is easy, so it's tempting to forget that there's a huge performance cost for adding to the DOM repeatedly. If you're adding many elements to the same container, you'll want to concatenate all the HTML into a single string, and then append that string to the container instead of appending the elements one at a time. Use an array to gather all the pieces together, then join them into a single string for appending:

Why do we have to use an array? What's wrong with just repeatedly appending to a string directly? Isn't string concatination suppose to be faster than array join?

Answer:1

Looking at the page you linked, I think you are asking why is it better to do

var myItems = [];
var myList = $( "#myList" );

for ( var i = 0; i < 100; i++ ) {
    myItems.push( "<li>item " + i + "</li>" );
}

myList.append( myItems.join( "" ) );

instead of

var myItems = "";
var myList = $( "#myList" );

for ( var i = 0; i < 100; i++ ) {
    myItems += "<li>item " + i + "</li>";
}

myList.append( myItems );

This is, in my opinion, entirely subjective, and the thing you quoted is not really a recommendation for array appending or string concatenation. The main benefit to using arrays is actually tangential in that it's easier to manipulate an array and write maintainable and readable code than it is to do the same with a string.

The other answers seem to be answering why is it bad to do

var myItems = "";
var myList = $( "#myList" );

for ( var i = 0; i < 100; i++ ) {
    myList.append( "<li>item " + i + "</li>" );
}

And yeah, that's because calling myList.append() over and over is repeatedly modifying the DOM and that can become very very slow. Calling it once would be much faster.

Answer:2

The performance cost exists because the innerHTML property on DOM elements doesn't behave like a simple plain-object property update. Every time you set the value of innerHTML, the browser does magic behind the scenes. It has to go through the trouble of firing up the HTML parser and generating those new tags, replacing whatever node tree was there previously.

The anti-pattern element.innerHTML += string is one of the worst because that causes the browser to first stringify all of the HTML within the target element, append a new string to the end of that HTML, and then parse that entire node tree all over again even before reaching whatever else you've added.

This is incredibly painstaking, to say the least.

Concatenation is totally fine if you are using a local variable. The point is, never concatenate on the innerHTML property directly.

var html = '<p>stuff</p>'
html += '<p>more stuff</p>'
html += '<p>even more stuff</p>'
html += '<p>extra stuff</p>'
element.innerHTML = html

However, if you need to update the DOM immediately and/or frequently, prefer creating and appending new nodes over innerHTML. For example,

var element = document.createElement('p')
element.textContent = 'Hello World'
document.body.appendChild(element)
Answer:3

I'm new to JS and JQuery, and I'm implementing a simple web app: I used 16 pictures to make a map (4x4 grid). When the user clicks or releases the mouse, the console prints out the mouse's X and Y ...

I'm new to JS and JQuery, and I'm implementing a simple web app: I used 16 pictures to make a map (4x4 grid). When the user clicks or releases the mouse, the console prints out the mouse's X and Y ...

I've created this little fancy jQuery Snippet to toggle the class of an element in an interval: setInterval(function(){$('.grid-item .slide-image').toggleClass('active')}, 800); The script works ...

I've created this little fancy jQuery Snippet to toggle the class of an element in an interval: setInterval(function(){$('.grid-item .slide-image').toggleClass('active')}, 800); The script works ...

  1. jquery toggle class onclick
  2. jquery toggle class animation
  3. jquery toggleclass not working
  4. jquery toggle class on hover
  5. jquery toggle class multiple
  6. jquery toggle class based on boolean
  7. jquery toggle class on button click
  8. jquery toggleclass delay
  9. jquery toggle class hide show
  10. jquery toggle class codepen
  11. jquery toggle class on scroll
  12. jquery toggle class condition
  13. jquery toggle class function
  14. jquery toggle class click
  15. jquery toggle class callback
  16. jquery toggle class by id
  17. jquery toggleclass off
  18. jquery toggle class visibility
  19. jquery toggle class this
  20. jquery toggle class on focus

I am trying to make the angular material directive to render vertically. I have tried using transform:rotate in the CSS but the slider starts behaving and rendering differently. md-slider { ...

I am trying to make the angular material directive to render vertically. I have tried using transform:rotate in the CSS but the slider starts behaving and rendering differently. md-slider { ...

  1. make angular material table responsive
  2. make angular material responsive
  3. make angular material input smaller
  4. how to make angular material dialog draggable
  5. how to make angular material table scrollable
  6. how to make angular material grid responsive
  7. make checkbox readonly angular material
  8. angular material make button smaller
  9. angular material make card clickable
  10. angular material make div scrollable
  11. angular material make icon bigger
  12. angular material make list scrollable
  13. angular material make button bigger
  14. angular material make table editable
  15. angular material make header sticky

Why does this transcluded directive repeat Name inside directive = Frank twice? I thought I understood transclusion but this has me confused. Here's a fiddle <div ng-app="myApp" ng-controller="...

Why does this transcluded directive repeat Name inside directive = Frank twice? I thought I understood transclusion but this has me confused. Here's a fiddle <div ng-app="myApp" ng-controller="...