<div id="teste">teste</div>
var teste_n = '123px';
var _style = window.getComputedStyle(document.getElementById('teste'));
document.getElementById('teste').closest('[hidden]');
document.getElementById('teste').offsetwidth;
document.getElementById('teste').hidden;
let teste = document.getElementById('teste');
teste.offsetWidth && teste.offsetHeight;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
closest | |
offsetwidth | |
hidden | |
offsetWidth && offsetHeight |
Test name | Executions per second |
---|---|
closest | 3518286.5 Ops/sec |
offsetwidth | 6622458.5 Ops/sec |
hidden | 6157999.0 Ops/sec |
offsetWidth && offsetHeight | 404109.9 Ops/sec |
Let's break down the benchmark and its various components.
Benchmark Definition JSON
The provided JSON represents a JavaScript microbenchmark that compares different approaches to retrieve properties from an HTML element.
Script Preparation Code
This section of code is executed once, before running each test. It:
'123px'
to the variable teste_n
.teste
using the window.getComputedStyle()
function and stores it in the variable _style
.Html Preparation Code
This section of code is executed once, before running each test. It creates a simple HTML structure:
<div id="teste">teste</div>
This div will be used to store the test element.
Individual Test Cases
The benchmark consists of four test cases:
closest
: Retrieves the closest ancestor with the hidden
pseudo-class using the closest()
method.offsetwidth
: Retrieves the offset width property using the .offsetWidth
property (note: there is a typo in the original code, it should be .offsetWidth
, not .offsetwidth
).hidden
: Retrieves the value of the hidden
pseudo-class directly using the .hidden
property.offsetWidth && offsetHeight
: Verifies that the test element has both an offset width and height using a conditional statement.Library Usage
None of these tests explicitly use a JavaScript library, but some modern browsers may utilize internal libraries or APIs to implement these methods (e.g., closest()
is often implemented in terms of querySelectorAll(), while .offsetWidth
and .hidden
are part of the DOM API).
Special JS Feature or Syntax
There is no special JS feature or syntax used in this benchmark.
Other Considerations
These tests can be seen as examples of:
offsetWidth
, .offsetWidth
hidden
, [hidden]
closest
In terms of optimization, these tests aim to measure the performance differences between:
.offsetWidth
)offsetWidth
)offsetWidth && offsetHeight
)Alternative Approaches
Some alternative approaches could be explored, such as:
getComputedStyle()
instead of window.getComputedStyle()
CSSOMView
or similar APIs for optimized style accessHowever, these alternatives may not necessarily provide significant performance improvements over the existing implementation.
Benchmarking Insights
The provided benchmark results show that:
closest()
method is slower than retrieving properties directly (.offsetWidth
).hidden
pseudo-classes using .hidden
is faster than relying on [hidden]
.offsetWidth && offsetHeight
) does not provide significant performance gains compared to separate property accesses.