expandostore-getset

JavaScript performance comparison

Test case created

Preparation code

<script>
var domCache = {},
    domExpandoAttr = 'dom'+ (new Date()).getTime(),
    domId = 1;

function domNextId(){ return domId++; }

function DOMExpandoStore(element, key, value) {
  var expandoId = element[domExpandoAttr],
      expandoStore = domCache[expandoId || -1];

  if(value != null) {
    if(!expandoStore) {
      element[domExpandoAttr] = expandoId = domNextId();
      expandoStore = domCache[expandoId] = {};
    }
    expandoStore[key] = value;
  } else {
    return expandoStore && expandoStore[key];
  }
}

function DOMExpandoStoreSet(element, key, value) {
  var expandoId = element[domExpandoAttr];
  ( (!expandoId)
      ? (element[domExpandoAttr] = expandoId = domNextId(), domCache[expandoId] = {})
      : domCache[expandoId]
  )[key] = value;
}

function DOMExpandoStoreGet(element, key) {
  var expandoId = element[domExpandoAttr];
  return expandoId && domCache[expandoId][key];
}
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
DOMExpandoStore
var el = document.createElement('div');
DOMExpandoStore(el, 'one', 'two');
DOMExpandoStore(el, 'two', 'three');
DOMExpandoStore(el, DOMExpandoStore(el, 'one')).length;
DOMExpandoStore(el, 'two').length;
pending…
DOMExpandoStoreGet/Set
var el = document.createElement('div');
DOMExpandoStoreSet(el, 'one', 'two');
DOMExpandoStoreSet(el, 'two', 'three');
DOMExpandoStoreGet(el, DOMExpandoStoreGet(el, 'one')).length;
DOMExpandoStoreGet(el, 'two').length;
 
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. Here’s a list of current revisions for this page:

0 comments

Add a comment