Created
February 19, 2014 00:12
-
-
Save caccialdo/9083423 to your computer and use it in GitHub Desktop.
DOM creation & update benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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