Last active
April 30, 2016 00:23
-
-
Save hattmarris/adc822c8da62f5440821842ae6454ca6 to your computer and use it in GitHub Desktop.
Test attribute vs id selection
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
var id = getRandomInt(); | |
var div = document.createElement('div'); | |
div.setAttribute('id', id); | |
var reps = 100; | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min)) + min; | |
} | |
function selectByAttribute(reps) { | |
console.time('selectByAttribute'); | |
for(i=0; i<reps; i++) { | |
document.querySelectorAll('[id=' + id + ']'); | |
} | |
console.timeEnd('selectByAttribute'); | |
} | |
function selectById(reps) { | |
console.time('selectById'); | |
for(i=0; i<reps; i++) { | |
document.querySelectorAll('#'+id); | |
} | |
console.timeEnd('selectById'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment