<div id="foo"></div>
var element = document.getElementById("foo");
element.style.display = "none";
var element = document.getElementById("foo");
element.setAttribute("style", "display:none");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
style➜display | |
setAttribute➜display |
Test name | Executions per second |
---|---|
style➜display | 2479284.5 Ops/sec |
setAttribute➜display | 4126750.2 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares two ways to set the display
property of an HTML element: using CSS styles (style.display
) versus using the setAttribute
method with a string argument containing the style value (setAttribute('style', 'display:none')
).
Options Being Compared
There are two options being compared:
display
property directly on the element's stylesheet using element.style.display = "none"
.style
with the value display:none
and then parsing it to set the display property using element.setAttribute('style', 'display:none')
.Pros and Cons
Here are some pros and cons of each approach:
Library Usage
In this benchmark, no libraries are explicitly mentioned. However, the document.getElementById
method is used, which is part of the DOM (Document Object Model) API.
Special JavaScript Features/Syntax
There's no special JavaScript feature or syntax being tested in this benchmark. The code uses standard JavaScript syntax and features, such as variable declarations, string interpolation, and function calls.
Alternative Approaches
Other approaches to setting the display
property of an HTML element might include:
style.cssText
property instead of style.display
.className
property.It's worth noting that these alternative approaches might have different performance characteristics, code readability, and maintainability trade-offs compared to the style-based and attribute-based approaches being tested in this benchmark.