<style type="text/css">
.hide {
display: none;
}
.pos {position: absolute !important;
top: -9999px !important;
left: -9999px !important;
}
</style>
<div id="container"></div>
var container = document.getElementById('container');
var item;
for (let i = 0; i < 100; i += 1) {
item = document.createElement('div')
const ul = document.createElement('ul');
let html = [];
for (let j = 0; j < 2000; j += 1) {
html.push(`<li>${j}</li>`);
}
ul.innerHTML = html.join('');
item.appendChild(ul);
container.appendChild(item)
}
let i = 3000;
while (i--) {
item.classList.add("hide");
item.classList.remove("hide");
}
let i = 3000;
while (i--) {
item.classList.add("pos");
item.classList.remove("pos");
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
show hide by display | |
show hide by position |
Test name | Executions per second |
---|---|
show hide by display | 289.4 Ops/sec |
show hide by position | 278.0 Ops/sec |
I'll break down the provided benchmark definition and individual test cases to explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The provided JSON represents a JavaScript microbenchmarking script that creates a container element with multiple div
elements inside it. Each div
contains an unordered list (ul
) with 2000 list items. The benchmark tests two approaches:
display
CSS property to hide and show the elements.position
CSS property to position absolute and then remove that positioning.Options Compared
The two options being compared are:
display
CSS property. This approach relies on the browser's layout engine to reflow and repaint the element when its display state changes.Pros and Cons
Both approaches have their pros and cons:
display
property of an element.Libraries and Special JS Features
In this benchmark, there is no specific library being used (e.g., jQuery), but the document.createElement
method is used to create DOM elements. This is a standard JavaScript feature.
Device Platform and Browser
The latest benchmark result shows Chrome 95 running on a Windows Desktop platform. The test case uses the RawUAString
field, which contains information about the browser's User Agent string (e.g., version number, platform).
Other Alternatives
To improve or modify this benchmark, you could consider:
Keep in mind that benchmarking is an ongoing process, and results may vary depending on the specific hardware, software, and usage patterns.