Skip to content

Instantly share code, notes, and snippets.

@hattmarris
Last active April 30, 2016 00:23
Show Gist options
  • Save hattmarris/adc822c8da62f5440821842ae6454ca6 to your computer and use it in GitHub Desktop.
Save hattmarris/adc822c8da62f5440821842ae6454ca6 to your computer and use it in GitHub Desktop.
Test attribute vs id selection
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