<div id="testElement"><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div><div id="trueElement" myattribute="trueElement"></div><div id="testElement"></div><div id="testElement"></div><div id="testElement"></div></div>
var el = document.getElementById('trueElement');
var className = el.className;
var el = document.querySelector('[myattribute="trueElement"]');
var className = el.className;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getElementById | |
querySelector |
Test name | Executions per second |
---|---|
getElementById | 4371547.5 Ops/sec |
querySelector | 756972.8 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
What is being tested?
The provided JSON represents a JavaScript microbenchmark that compares two approaches for selecting HTML elements: document.getElementById
versus document.querySelector
.
Options compared:
Two options are being compared:
document.getElementById
: This method retrieves an element by its ID.document.querySelector
: This method retrieves the first element that matches a given CSS selector.Pros and Cons of each approach:
document.getElementById
:document.querySelector
:getElementById
, as it allows selecting elements based on various CSS selectors.getElementById
due to the overhead of parsing the CSS selector.Library usage:
None of the provided benchmarks use any external libraries. They rely solely on the built-in JavaScript DOM APIs.
Special JS features or syntax:
The benchmark uses a simple CSS attribute selector ([myattribute="trueElement"]
) in the querySelector
test case. This is a valid CSS selector that matches elements with an attribute named "myattribute" and value "trueElement". The exact syntax might vary depending on the browser being tested.
Alternative approaches:
There are other methods for selecting HTML elements, such as:
document.querySelectorAll
: Retrieves multiple elements that match a given CSS selector.querySelectorAll
with a callback function: Applies a callback function to each matched element.Element.prototype.querySelectorAll
: A more concise way of selecting elements in some browsers (e.g., Edge).However, these alternatives are not being compared in the provided benchmark.