JavaScript How to avoid appending elements to DOM in a loop

My function is like below. Consider for high performance, how can I avoid to add text in a loop structure in html? Please advise. Thank you so much.

function  createDiv (array) {
    var i;
    var target = document.getElementById("container");
    for(i = 0; i < array.length; i++) {       
        target.appendChild("<div>" + array[i] + "</div>");
    }
}
Answer:1

You can create an element outside the loop, and inside the loop append to the element (not touching the DOM). Then after the loop finishes, append the new object to the html.

Answer:2

I have an assignment to create a battleship game, with 3x3 - 10x10 grid. My current idea would be to create a grid with a click of a button, or atlest change the current size with a click of a button. ...

I have an assignment to create a battleship game, with 3x3 - 10x10 grid. My current idea would be to create a grid with a click of a button, or atlest change the current size with a click of a button. ...

  1. create specific size file linux
  2. create specific size image
  3. create specific size file windows
  4. create specific size file
  5. create pdf specific size
  6. create a specific size rectangle in photoshop
  7. create list specific size python
  8. create partition specific size diskpart
  9. create dataframe specific size
  10. create file specific size powershell
  11. create vector specific size
  12. word create specific size
  13. create array of specific size python
  14. create array with specific size javascript
  15. create file of specific size mac
  16. create array of specific size matlab
  17. create array of specific size java
  18. create vector of specific size c++
  19. create arraylist of specific size
  20. create matrix with specific size matlab

I am creating a dynamic refresh feature for div elements on my site that need to be dynamically refreshed. $(document).ready(function() { function refreshDiv() { var refreshUrl =...

I am creating a dynamic refresh feature for div elements on my site that need to be dynamically refreshed. $(document).ready(function() { function refreshDiv() { var refreshUrl =...

  1. jquery bind elements

I'm looking to purge Cloudflare's cache through its API. More specially, the purge all files command. However, I keep running into the "Invalid Content-Type header, valid values are application/json,...

I'm looking to purge Cloudflare's cache through its API. More specially, the purge all files command. However, I keep running into the "Invalid Content-Type header, valid values are application/json,...

  1. purge cloudflare cache
  2. cloudflare purge cache api
  3. cloudflare purge cache not working
  4. cloudflare purge cache api php
  5. cloudflare purge cache wildcard
  6. cloudflare purge cache by url
  7. failed to purge cloudflare cache not authenticated
  8. how to purge cloudflare cache

We are building a form-based app that has a complex object with many levels of nested properties. So far, I have created a simple experiment with a single view model with one object. The experiment ...

We are building a form-based app that has a complex object with many levels of nested properties. So far, I have created a simple experiment with a single view model with one object. The experiment ...

  1. bind view model in mvc
  2. bind view model to view
  3. bind viewmodel extjs
  4. wpf bind to viewmodel
  5. bind view viewmodel xaml
  6. bind view to model backbone
  7. aurelia bind view-model
  8. bind partial view model
  9. bind property view model
  10. wpf bind viewmodel property
  11. bind view to viewmodel
  12. bind view to viewmodel android
  13. bind view to viewmodel wpf
  14. prism bind view viewmodel
  15. bind button to viewmodel
  16. android bind view to model
  17. bind button click to view model
  18. angular bind view to model
  19. razor bind view to model
  20. mvvm bind view to model