JavaScript · Middle · Question 44 of 75

How do you efficiently insert 1000 elements into the DOM?

Explanation

Each insertion into the live DOM can trigger style and layout recalculation. A fragment is built outside the document and inserted at once; el.append(...nodes) works the same way. innerHTML += in a loop reparses all the markup each time and loses handlers.

Further reading

Problem with a question

JavaScript · Middle · question 44

Test yourself

Take the JavaScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz