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

Revision 90 of this benchmark created on


Preparation HTML

<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<input type="text" id="theInput" value="foo"></input>
<script>
  $.fn.extend({
    dat: function(key, value) {
        var retVal = $.data(this[0], key, value);
        return (value !== undefined || typeof key === "object") ? this : retVal;
    },
    hasDat: function(key) {
        return $.hasData(this[0], key);
    },
    removeDat: function(key) {
        $.removeData(this[0], key);
        return this;
    }
  });
  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
Fast data set
$dom.dat('someData','test1');
ready

Revisions

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