Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
setAttribute vs direct vs []
(version: 0)
Comparing performance of:
setAttribute vs direct vs []
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<textbox id="text"></textbox>
Tests:
setAttribute
var element = document.getElementById("text"); var i = 10000; while (i--) { element.setAttribute("value", i); }
direct
var element = document.getElementById("text"); var i = 10000; while (i--) { element.value = i; }
[]
var element = document.getElementById("text"); var i = 10000; while (i--) { element["value"] = i; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
setAttribute
direct
[]
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
28 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
setAttribute
341.1 Ops/sec
direct
195339.3 Ops/sec
[]
198378.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Purpose:** The benchmark measures the performance of three different approaches to setting the value of an HTML input element: 1. `setAttribute` 2. Direct assignment (`element.value = i`) 3. Property access with square brackets (`element["value"] = i`) **Options Comparison:** * **`setAttribute`**: This method uses the `setAttribute` method to set the value attribute of the input element. It takes two arguments: the attribute name and the value. * **Direct Assignment**: This method assigns the value directly to the `value` property of the input element. * **Property Access with Square Brackets**: This method accesses the `value` property of the input element using square brackets (`[]`). The syntax is `element["value"] = i`. **Pros and Cons:** * **`setAttribute`**: + Pros: Can be used to set any attribute on an element, not just the value. + Cons: May be slower due to the method call overhead. * **Direct Assignment**: Fastest way to set a property directly on an object. * **Property Access with Square Brackets**: Similar to direct assignment, but may incur additional overhead due to the bracket notation. **Library and Syntax:** None of the options require any external libraries or special JavaScript features. They are all basic syntax for working with HTML elements in JavaScript. **Other Considerations:** * In modern browsers, the `value` property is a setter function that calls the `setAttribute` method internally. This means that direct assignment (`element.value = i`) may be equivalent to `setAttribute` in terms of performance. * Property access with square brackets (`element["value"] = i`) can be slower due to the overhead of resolving the bracket notation. **Benchmark Results:** The benchmark results show that: * Direct assignment (`element.value = i`) is the fastest approach, followed closely by property access with square brackets (`element["value"] = i`). * `setAttribute` is the slowest approach. Keep in mind that these results may vary depending on the specific browser, version, and hardware configuration.
Related benchmarks:
id vs setAttribute
element.getAttribute("name") vs element.attributes.name.value
disabled=true vs setAttribute("disabled", "");
Setting CSS: Direct CSS property vs setAttribute("style",...)
Comments
Confirm delete:
Do you really want to delete benchmark?