data attributes

JavaScript performance comparison

Test case created by Stoyan

Preparation code

<div id="mydiv"></div>
Benchmark.prototype.setup = function() {
    var div = document.getElementById('mydiv');
    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];

Benchmark.prototype.teardown = function() {
    div.setAttribute('data-po', null);
    div.setAttribute('data-ta', null);
    div.setAttribute('data-to', null);
    div.setAttribute('__data', null);

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
old school setAttribute
div.setAttribute('data-po', 'to');
div.setAttribute('data-ta', 'ma');
div.setAttribute('data-to', 'to');
var a = div.getAttribute('data-po');
var b = div.getAttribute('data-ta');
var c = div.getAttribute('data-to');
DIY Data
Data.set(div, {po: 'to', ta: 'ma', to: 'to'});
var data = Data.get(div);
var a = data.po;
var b = data.ta;
var c =;
HTML5 dataset
div.dataset.po = 'to';
div.dataset.ta = 'ma'; = 'to';
var data = div.dataset;
var a = data.po;
var b = data.ta;
var c =;

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Comment form temporarily disabled.

Add a comment