<div id="root"><div>
var el = document.getElementById("root");
function getStyle(elem){
return elem.ownerDocument.defaultView.getComputedStyle( elem );
}
function getStyle2(elem){
return window.getComputedStyle( elem );
}
var computedStyle = getStyle(el);
var computedStyle = getStyle2(el);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
ownerDocument.defaultView | |
window.getComputedStyle |
Test name | Executions per second |
---|---|
ownerDocument.defaultView | 1899320.9 Ops/sec |
window.getComputedStyle | 1618832.6 Ops/sec |
Let's dive into the benchmark being measured on MeasureThat.net.
What is being tested?
The benchmark is comparing two approaches to access the computed style of an element:
elem.ownerDocument.defaultView.getComputedStyle(elem)
window.getComputedStyle(elem)
These two approaches are testing how efficient and accurate each method is in retrieving the computed style of an element, which includes properties like width, height, color, etc.
Options compared:
There are two options being compared:
A) Using the ownerDocument.defaultView
object to access the global CSSOM (CSS Object Model) API.
B) Using the window.getComputedStyle()
method directly on the window object.
Pros and Cons of each approach:
elem.ownerDocument.defaultView.getComputedStyle(elem)
ownerDocument
property on the element, which may introduce additional overhead or dependencies.window.getComputedStyle(elem)
ownerDocument
property.Library/Tool usage:
There is no specific library being used in this benchmark, but it relies on the JavaScript Engine and Browser implementations of the getComputedStyle()
method. The ownerDocument
property is a part of the DOM (Document Object Model) API, which is implemented natively by most browsers.
Special JS feature or syntax:
There are no special JS features or syntax being used in this benchmark beyond standard ECMAScript 2020 language features.
Other alternatives:
Some alternative approaches to accessing the computed style of an element include:
getComputedStyle()
method on a separate CSSStyleRule object, if available.Keep in mind that these alternatives may have different performance implications, trade-offs, or requirements depending on the specific use case.