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

Revision 19 of this benchmark created on


Description

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

Preparation HTML

<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script><input type="text" id="btn"></div>

Setup

function s(elem, attr, val) {
      elem.setAttribute(attr, val);
    }
    
    function gebi(id) {
      return document.getElementById('id');
    }

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
Wrapped native setAttribute
s(document.getElementById('btn'), "disabled", "disabled");
ready
is wrapped native document.getElementById function expensive ?
gebi('btn').setAttribute("disabled", "disabled");
ready

Revisions

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