jQuery.data vs jQuery().data vs .getAttribute (v49)

Revision 49 of this benchmark created by Dennis Kim on


Description

Compare various methods of setting and getting HTML5 data attributes.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="el"></div>
<script>
  var $el = $('#el');
  var el = document.getElementById('el');
  var temp;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery.fn.data
$el.data('key', 'val-' + Math.floor(Math.random() * 1001));
temp = $el.data('key');
ready
jQuery.data
$.data($el, 'key', 'val-' + Math.floor(Math.random() * 1001));
temp = $.data($el, 'key');
ready
setAttribute
el.setAttribute('data-key', 'val-' + Math.floor(Math.random() * 1001));
temp = el.getAttribute('data-key');
ready
accessor
el['data-key'] = 'val-' + Math.floor(Math.random() * 1001);
temp = el['data-key'];
ready

Revisions

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