Skip to content

Instantly share code, notes, and snippets.

@caccialdo
Created February 19, 2014 00:12
Show Gist options
  • Save caccialdo/9083423 to your computer and use it in GitHub Desktop.
Save caccialdo/9083423 to your computer and use it in GitHub Desktop.
DOM creation & update benchmark
(function () {
// Creation V1
console.time('v1');
var s = document.createElement('select'),
i, html = '';
s.multiple = true;
for (i = 0; i < 5000; i++) {
html += '<option selected value="' + i + '">id #' + i + '</option>';
}
s.innerHTML = html;
document.body.appendChild(s);
console.timeEnd('v1');
// Update
console.time('update');
Array.prototype.forEach.call(s.querySelectorAll('option'), function (opt) {
opt.selected = false;
});
console.timeEnd('update');
})();
(function () {
// Creation V2
console.time('v2');
var s = document.createElement('select'),
i, opt;
s.multiple = true;
document.body.appendChild(s);
for (i = 0; i < 5000; i++) {
opt = document.createElement('option');
opt.selected = true;
opt.value = i;
opt.innerHTML = 'id #' + i;
s.appendChild(opt);
}
console.timeEnd('v2');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment