JQuery: is .data() faster than .attr("data-...") (v2)

Revision 2 of this benchmark created by Jonas Middendorf on


Description

When you have an attribute data-... in a HTML element: Is it faster to get the value using $.data('...') or attr('data-...')

Preparation HTML

<script src="http://code.jquery.com/jquery-2.0.0b2.min.js"></script>
<div id="myDiv" data-my-data="value"></div>

Setup

var $myDiv = $('#myDiv')

Test runner

Ready to run.

Testing in
TestOps/sec
$.data().myData
$myDiv.data().myData
ready
$.attr('data-my-data')
$myDiv.attr('data-my-data')
ready
$.data('my-data')
$myDiv.data('my-data')
ready

Revisions

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

  • Revision 1: published by JQuery: is .data() faster than .attr("data-...") on
  • Revision 2: published by Jonas Middendorf on
  • Revision 3: published on
  • Revision 4: published on
  • Revision 5: published on
  • Revision 6: published on