jQuery .data() performance vs .attr() performance vs elem.getAttribute() (v30)

Revision 30 of this benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<input type="text" id="theInput" value="foo"></input>
<input type="text" id="theInput2" value="foo" myAttr="1234" data-my-attr="1234"></input>
<script>
  var dom= document.getElementById("theInput");
  var $dom= $(dom);
  $dom.attr('someAttr', 'theValue').data('someData', 'theValue');
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Attribute set
dom.setAttribute('someAttr','test1');
ready
Data set
$dom.data('someData','test1');
ready
Direct data set
$.data($dom, 'someData','test1');
ready
jQuery attr() set
$dom.attr('someAttr','test1');
ready
Attribute set with jQuery object
if ($dom.length > 0)
$dom[0].setAttribute('someAttr','test1');
ready
attr() get
var test = $dom.attr('someAttr')
ready
data() get
var test = $dom.data('someData')
ready
attribute get
if ($dom.length > 0)
var test = $dom[0].getAttribute('someAttr');
ready
$.data get
var test = $.data($dom[0],'someAttr');
ready
set prop
$dom[0].someAttr = "test1";
ready
get prop
var test = $dom[0].someAttr;
ready
HTML Element
var text = $('#theInput2');
var answer = text[0].attributes.myattr.value;
ready
DATA
var text = $('#theInput2');
var answer = text.data('myattr');
ready

Revisions

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