Scalable div appending

Ridiculously Slow – being silly

for (var i = 0; i < 65000; i++) {
  document.body.appendTo(document.createElement("div"));
}

Fast – using a DocumentFragment

var fragment = document.createDocumentFragment();

for (var i = 0; i < 65000; i++) {
  fragment.appendChild(document.createElement("div"));
}

document.body.appendChild(fragment);

Probably obvious for most, but not for me on a Friday evening after working all day. 😛

Published by

oughton

Senior software engineer with extensive experience in enterprise software development targeting wholesale financial organisations and cloud solution providers.

Leave a Reply

Your email address will not be published. Required fields are marked *