setAttribute src vs attribute assign

JavaScript performance comparison

Test case created by Paul Comanici (darkyndy)

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">

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
src assign
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"
}
pending…
setAttribute
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");
}
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:

0 comments

Add a comment