Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
className vs. setAttribute vs. classList on body
(version: 0)
Comparing performance of:
className vs setAttribute vs classList
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<body></body>
Tests:
className
var i = 1000; while (i--) { document.body.className = "bar"; }
setAttribute
var i = 1000; while (i--) { document.body.setAttribute("class", "bar"); }
classList
var i = 1000; while (i--) { document.body.classList.add("bar"); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
className
setAttribute
classList
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **What is being tested?** The provided JSON represents a benchmark that tests three different approaches to dynamically set the `className` attribute on an HTML `<body>` element: 1. **`className` property**: This approach sets the `className` property directly on the `<body>` element. 2. **`setAttribute` method**: This approach uses the `setAttribute` method of the `<body>` element to set its `class` attribute (note that this is different from `className`, which is a shorthand for multiple classes). 3. **`classList` API**: This approach uses the `classList` API to add a class name (`"bar"` in this case) to the `<body>` element. **Options compared** The three approaches are being tested against each other, and their performance differences will be measured. **Pros and Cons of each approach:** 1. **`className` property**: * Pros: More concise and readable way to set class names. * Cons: Might not work as expected in older browsers that don't support the shorthand syntax. 2. **`setAttribute` method**: * Pros: Works across all browsers, but can be less readable than other approaches. * Cons: Requires specifying both `class` and `value` attributes, which can lead to more code. 3. **`classList` API**: * Pros: More efficient and modern way to manage classes, with better browser support. * Cons: Might require additional imports or setup if not already available. **Library usage** None of the test cases use any external libraries. The `classList` API is a native JavaScript API introduced in ECMAScript 2015 (ES6). **Special JS feature or syntax** The only special feature used here is the shorthand syntax for setting class names on an element, which is supported by most modern browsers but might not work in older versions. **Other alternatives** If you want to test additional approaches, some other options could include: * Using CSS classes instead of JavaScript properties * Utilizing browser-specific APIs (e.g., Chrome's `addStyleClass` method) * Implementing custom class management logic However, these alternative approaches might not be as widely supported or readable as the `className`, `setAttribute`, and `classList` methods used in this benchmark.
Related benchmarks:
Native JS classList versus Native JS getAttribute
classList.add vs setAttribute
DataAttribute vs Class Selector on body
setAttribute vs classList.add
Comments
Confirm delete:
Do you really want to delete benchmark?