setAttribute src vs attribute assign
JavaScript performance comparison
Info
imageElement.setAttribute("src", "new src") vs imageElement.src = "new src"
Preparation code
<img id="testSrcAssign" src="http://i3.ytimg.com/vi/uY-HU2Xh7PE/3.jpg">
<script>
Benchmark.prototype.setup = function() {
function testSetAttribute() {
var i = 0,
imgEl = document.getElementById("testSrcAssign");
for (i = 3; i > 0; i = i - 1) {
imgEl.setAttribute("src", "http://i3.ytimg.com/vi/uY-HU2Xh7PE/" + i + ".jpg");
}
}
function testAssignSrc() {
var i = 0,
imgEl = document.getElementById("testSrcAssign");
for (i = 3; i > 0; i = i - 1) {
imgEl.src = "http://i3.ytimg.com/vi/uY-HU2Xh7PE/" + i + ".jpg"
}
}
};
</script>
Preparation code output

Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
src assign |
|
pending… |
setAttribute |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Paul Comanici (darkyndy)
- Revision 2: published by Paul Comanici (darkyndy)
0 comments