jQuery attr() vs. Native setAttribute() (v4)

Revision 4 of this benchmark created by cg on


Description

jQuery's attr() method will always have more overhead because of the extensibility added.

Preparation HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script><input type="text" id="btn"></div>

Setup

var $btn = $("#btn");
    var btn = document.getElementById('btn');

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery attr()
$("#btn").attr("disabled", "disabled");
ready
Native setAttribute()
document.getElementById('btn').setAttribute("disabled", "disabled");
ready
getElementById() with jQuery attr()
$(document.getElementById('btn')).attr("disabled", "disabled");
ready
jQuery selector with Native setAttribute
$('#btn')[0].setAttribute("disabled", "disabled");
ready
jQuery prop()
$("#btn").prop("disabled", "disabled");
ready
jQuery attr() without selector
$btn.attr("disabled", "disabled");
ready
Native setAttribute without selector
btn.setAttribute("disabled", "disabled");
ready
Native setAttribute without selector but from jQuery object
$btn[0].setAttribute("disabled", "disabled");
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.