attr vs $.data (plus permutations) (v38)

Revision 38 of this benchmark created on


Preparation HTML

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

Test runner

Ready to run.

Testing in
TestOps/sec
el.setAttribute
el.setAttribute('someAttr','test1');
ready
set $el.attr
$el.attr('someAttr','test1');
ready
set $el.data
$el.data('someData','test1');
ready
set $.data
$.data(el, 'someData','test1');
ready
el.getAttribute
var test = el.getAttribute('someAttr');
ready
get $el.attr
var test = $el.attr('someAttr')
ready
get $el.data
var test = $el.data('someData')
ready
get $.data
var test = $.data(el,'someData');
ready
get el.attributes.someAttr.value
var test = el.attributes.someAttr.value;
ready
get data-
var test = $el2.data('testAttr');
ready

Revisions

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