-
-
Save sschmidTU/a8f9a872efc2063a9365976fc26946e2 to your computer and use it in GitHub Desktop.
getElementsByClassName vs getElementById vs querySelector (http://jsbench.github.io/#6fdcc5a726220a85eb7c6a00e955d532) #jsbench #jsperf
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>getElementsByClassName vs getElementById vs querySelector</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains 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
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
window.generated_divs = 5000; | |
let root = document.createElement("div"); | |
for(let i = 0; i < generated_divs; i++) { | |
let el = document.createElement("div"); | |
el.id = `foo${i}`; | |
el.className = `foo${i}`; | |
el.setAttribute(`foo${i}`, ''); | |
root.appendChild(el); | |
} | |
document.body.appendChild(root); | |
}; | |
Benchmark.prototype.teardown = function () { | |
document.body.removeChild(root); | |
root = null; | |
}; | |
suite.add("getElementById", function () { | |
// getElementById | |
let i = Math.floor(Math.random() * window.generated_divs); | |
let element = document.getElementById(`foo${i}`); | |
}); | |
suite.add("getElementsByClassName", function () { | |
// getElementsByClassName | |
let i = Math.floor(Math.random() * window.generated_divs); | |
let element = document.getElementsByClassName(`foo${i}`)[0]; | |
// getElementsByClassName just returns a HTMLCollection, basically a search query, | |
// which needs to be evaluated e.g. with [0]. | |
}); | |
suite.add("querySelector('#id')", function () { | |
// querySelector('#id') | |
let i = Math.floor(Math.random() * window.generated_divs); | |
let element = document.querySelector(`#foo${i}`); | |
}); | |
suite.add("querySelector('.class')", function () { | |
// querySelector('.class') | |
let i = Math.floor(Math.random() * window.generated_divs); | |
let element = document.querySelector(`.foo${i}`); | |
}); | |
suite.add("querySelector('[attr]]')", function () { | |
// querySelector('[attr]]') | |
let i = Math.floor(Math.random() * window.generated_divs); | |
let element = document.querySelector(`[foo${i}]`); | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("getElementsByClassName vs getElementById vs querySelector"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
screenshots:
full screenshot of upper part:
https://user-images.githubusercontent.com/33069673/132558324-14831ed0-12bc-43e3-96bf-c140a339fa7a.png
This is a fork of a version where the
getElementsByClassName
benchmark was flawed because theHTMLCollection
wasn't evaluated ([0]
) missing, which suggested that it was faster thangetElementById
:https://gist.github.com/denilsonsa/6fdcc5a726220a85eb7c6a00e955d532