Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
direct style vs. setAttribute vs. classList
(version: 0)
Comparing performance of:
direct style vs setAttribute vs classList
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo"></div>
Tests:
direct style
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.style.postiion = "fixed"; }
setAttribute
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.setAttribute("class", "bar"); }
classList
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.classList.add("bar"); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
direct style
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 break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **What is being tested?** The benchmark tests three different ways to set the `position` property of an HTML element in JavaScript: 1. **Direct style**: Using the `style` property directly on the element. 2. **setAttribute**: Setting an attribute on the element using the `setAttribute` method. 3. **classList**: Using the `classList` API to add a class with the desired value. **What options are being compared?** The three options are being compared in terms of their performance, specifically how many executions per second (ExecutionsPerSecond) they produce. **Pros and Cons:** 1. **Direct style**: * Pros: Simple and straightforward, no additional overhead. * Cons: May be slower due to the number of DOM operations required. 2. **setAttribute**: * Pros: Can be faster than direct style since it only sets a single attribute. * Cons: Requires an extra method call and may incur additional overhead. 3. **classList**: * Pros: Efficient and fast, as it only adds a class to the element's class list. * Cons: May require additional library or framework support. **Library/ Framework Consideration** The `classList` API is a modern JavaScript feature that provides an efficient way to work with classes on HTML elements. It's supported by most modern browsers and frameworks, making it a good choice for performance-critical applications. **Special JS Feature/Syntax** None of the benchmarked code uses special JavaScript features or syntax beyond standard ES6+ syntax. **Other Alternatives** For setting the `position` property directly on an element: * Using the `style.position` property with the `inline-style` CSS rule (although this is less efficient than direct style) * Using a library like jQuery to set the position property These alternatives may incur additional overhead or require more complex setup, making them less suitable for performance-critical applications. **Benchmark Preparation Code** The provided HTML preparation code `<div id="foo"></div>` creates an empty `div` element with the ID "foo", which is used as a target for the benchmarking tests. For the script preparation code, each test case is identical and only sets up the initial conditions for the test: ```javascript var element = document.getElementById("foo"); var i = 1000; while (i--) { // The actual code being tested is inside this loop } ``` This code creates a reference to the `div` element with ID "foo" and initializes a counter variable `i`. The test then runs in a loop, executing the code inside it repeatedly.
Related benchmarks:
className vs. setAttribute vs. classList
setAttribute(...) vs. classList.add(...) vs classList.value vs className
classList.add vs setAttribute
DataAttribute vs Class Selector on body
setAttribute vs classList.add
Comments
Confirm delete:
Do you really want to delete benchmark?