jQuery .data() performance vs .attr() performance, dynamic vs static element (v71)

Revision 71 of this benchmark created by Cody on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<input type="text" data-something = "this is it" id="theInput" value="foo"></input>
<script>
  var theInput = $('#theInput');
  theInput.attr('someAttr', 'theValue').data('someData', 'theValue');
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Attribute get
theInput.attr('someAttr');
ready
Attribute set
theInput.attr('someAttr', 'newValue');
ready
Data get
theInput.data('someData');
ready
Data set
theInput.data('someData', 'newValue');
ready
Direct data get
$.data(theInput.get(0), 'someData');
ready
Direct data set
$.data(theInput.get(0), 'someData', 'newValue');
ready
Get preexisting data attr via el.attr("data-attr")
theInput.attr( "data-something" );
ready
Get preexisting data attr via el.data()
theInput.data( "something" );
ready

Revisions

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