<div id="foo"></div>
var i = 5000;
while (i--) {
checkDisplay('foo');
}
function checkDisplay(id) {
let element = document.getElementById(id);
return element.style.width;
}
var i = 5000;
while (i--) {
checkDisplay('foo');
}
function checkDisplay(id) {
return document.getElementById(id).getBoundingClientRect().width === 0;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
style | |
getBoundingClientRect |
Test name | Executions per second |
---|---|
style | 547.0 Ops/sec |
getBoundingClientRect | 242.7 Ops/sec |
Let's dive into the provided benchmark and explain what is tested, compared, and some pros and cons of each approach.
Benchmark Definition
The benchmark definition represents two test cases: style
and getBoundingClientRect
. Both tests aim to compare the performance of getting the width property of an element using window.style.width
versus document.getElementById(id).getBoundingClientRect().width
.
Script Preparation Code and Html Preparation Code
The script preparation code is empty, which means that the benchmark doesn't perform any additional setup or initialization before running the test. However, it's essential to note that this might affect the results.
The html preparation code provides a simple <div id="foo"></div>
element, which will be used as the target for the width measurement.
Individual Test Cases
There are two individual test cases:
style
: This test case runs a while loop 5000 times, calling the checkDisplay('foo')
function inside the loop. The function uses window.style.width
to get the width of the element and returns its value.getBoundingClientRect
: This test case is similar to the first one but uses document.getElementById(id).getBoundingClientRect().width
to get the width of the element.Library: checkDisplay
The checkDisplay
function is not a built-in JavaScript library, but rather a custom implementation used in both test cases. Its purpose is to measure the width of an element using either window.style.width
or document.getElementById(id).getBoundingClientRect().width
. This function is likely written by the benchmark creator to simplify the measurement process.
Comparison
The comparison between style
and getBoundingClientRect
test cases aims to determine which method is faster for measuring the width of an element. The results will show how many executions per second each method achieves, with higher values indicating better performance.
Pros and Cons:
window.style.width
:document.getElementById(id).getBoundingClientRect().width
:Special JS Feature/Syntax:
There are no special JavaScript features or syntax used in this benchmark. However, it's essential to note that the getBoundingClientRect
method was introduced in HTML5 and might not be supported in older browsers.
Other Alternatives:
If you were to create a similar benchmark, other alternatives could include:
getComputedStyle()
or window.getComputedStyle()
Keep in mind that the choice of approach depends on your specific use case, performance requirements, and target browsers.