Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
setAttribute vs src
(version: 0)
Comparing performance of:
setAttribute vs style
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<img id="foo"/>
Tests:
setAttribute
var element = document.getElementById("foo"); var i = 10000; while (i--) { element.setAttribute("src", "https://blog-cloudflare-com-assets.storage.googleapis.com/2019/06/logo-cloudflare-dark-1.svg"); }
style
var element = document.getElementById("foo"); var i = 10000; while (i--) { element.src = "https://blog-cloudflare-com-assets.storage.googleapis.com/2019/06/logo-cloudflare-dark-1.svg"; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
setAttribute
style
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
setAttribute
20.5 Ops/sec
style
20.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmark preparation code to understand what is being tested. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches: setting the `src` attribute using the `setAttribute` method versus assigning a value directly to the `src` property. The test case involves creating an HTML image element, looping 10,000 times, and within each iteration, either sets the `src` attribute or assigns a new value to the `src` property. **Test Case Explanation** In the first test case (`"Test Name": "setAttribute"`), the code uses the `setAttribute` method to set the `src` attribute of the image element. This method takes two arguments: the attribute name and its value. In this case, it sets the `src` attribute to a specific URL. In the second test case (`"Test Name": "style"`), the code assigns a new value directly to the `src` property using the syntax `element.src = 'https://...';`. This is essentially setting the `src` attribute without using the `setAttribute` method. **Library and Syntax Considerations** The test cases use the `document.getElementById` function, which is part of the HTML DOM (Document Object Model) API. The purpose of this library is to allow web developers to dynamically access and manipulate HTML elements in a webpage. There are no special JavaScript features or syntax being tested here, as both approaches use standard JavaScript features. **Pros and Cons of Different Approaches** 1. **`setAttribute` approach:** * Pros: + Some older browsers may have performance issues with direct property assignment (see below). + May be considered more explicit and readable in some cases. * Cons: + Can lead to slower performance due to the additional overhead of calling a method. 2. **Direct Property Assignment (`element.src = '...'`) approach:** * Pros: + Typically faster, as it avoids the overhead of calling a method. * Cons: + May be less explicit and readable in some cases. **Performance Considerations** The performance difference between these two approaches is likely to be small for most use cases. However, if you're working with older browsers that have known performance issues with direct property assignment (e.g., IE), using `setAttribute` might be a safer choice. **Other Alternatives** If you want to test other approaches or variations on this theme, you could consider the following: * Using a different property, such as `href`, instead of `src`. * Setting multiple attributes at once by passing an object with attribute names and values. * Comparing performance with different types of data, such as strings versus objects, or numbers versus booleans. Keep in mind that these alternatives would require modifying the benchmark code to accommodate the new variations.
Related benchmarks:
id vs setAttribute
style➜display VS setAttribute➜display
Setting CSS: Direct CSS property vs setAttribute("style",...)
js - title vs getAttribute
Setattribute vs setAttribute with attribute already set
Comments
Confirm delete:
Do you really want to delete benchmark?