data-attr

JavaScript performance comparison

Revision 6 of this test case created by

Preparation code

<div id="div">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var div = document.getElementById('div');
  var Data = function() {
      var warehouse = {};
      var count = 1;
      return {
        reset: function() {
          count = 1;
          warehouse = {};
        },
        set: function(dom, data) {
          if (!dom.__data) {
            dom.__data = "hello" + count++;
          }
          warehouse[dom.__data] = data;
        },
        get: function(dom) {
          return warehouse[dom.__data];
        }
      };
      }();
</script>
      
<script>
Benchmark.prototype.teardown = function() {
  div.setAttribute('data-yo', null);
  div.setAttribute('data-ma', null);
  div.setAttribute('data-la', null);
  div.setAttribute('__data', null);
  Data.reset();

};
</script>

Preparation code output

<div id="div"> </div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
setAttribute
div.setAttribute('data-yo', 'yo');
div.setAttribute('data-ma', 'ma');
div.setAttribute('data-la', 'la');
var a = div.getAttribute('data-yo');
var b = div.getAttribute('data-ma');
var c = div.getAttribute('data-la');
pending…
Data
Data.set(div, {
  yo: 'yo',
  ma: 'ma',
  la: 'la'
});
var data = Data.get(div);
var a = data.yo;
var b = data.ma;
var c = data.la;
pending…
dataset
/* this is illegal: div.dataset = {yo: 'yo', ma: 'ma', la: 'la'}; */

/* fix end */
var data = div.dataset;
data.yo = 'yo';
data.ma = 'ma';
data.la = 'la';
var a = data.yo;
var b = data.ma;
var c = data.la;
pending…
jquery
var _div = $('div');
_div.attr('data-yo', 'yo');
_div.attr('data-ma', 'ma');
_div.attr('data-la', 'la');
var a = _div.attr('data-yo');
var b = _div.attr('data-ma');
var c = _div.attr('data-la');
pending…

Compare results of other browsers

Revisions

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

0 Comments