<div id="foo"></div>
var element = document.getElementById("foo");
element.setAttribute("bar", "");
var element = document.getElementById("foo");
element.classList.add("bar");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
setAttribute | |
classList |
Test name | Executions per second |
---|---|
setAttribute | 1943408.0 Ops/sec |
classList | 1477105.9 Ops/sec |
Let's break down what's being tested in this JavaScript microbenchmark.
Benchmark Purpose
The benchmark measures the performance difference between setting an attribute using setAttribute()
and adding a class to an element using classList.add()
on a HTML element with the id "foo".
Options Compared
setAttribute()
: This method sets the specified attribute(s) on an element.
classList.add()
: This method adds one or more classes to an element.
classList.remove()
) for more complex class management.Other alternatives:
style
attribute directly: This method can also set an attribute value, but it's less efficient and less readable than using setAttribute()
or classList.add()
.DOMTokenList
: Introduced in HTML5, this API allows you to manipulate classes more efficiently. However, its availability is limited to modern browsers.Library Usage In the provided benchmark code, there is no explicit library usage.
Special JS Feature/ Syntax
There are no special JavaScript features or syntax being tested in this benchmark. The focus is on comparing two basic method calls: setAttribute()
and classList.add()
.