Native Dojo Class Manipulation v HTML5

JavaScript performance comparison

Revision 8 of this test case created by

Preparation code

<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js">
</script>
<div id="empty_class">
</div>
<div class="a_class" id="single_class">
</div>
<div class="a_class b_class c_class" id="many_classes">
</div>
<script>
  require(['dojo/dom-class', 'dojo/dom', "dojo/has"], function(dClass, dDom, has) {
    domClass = dClass;
    dom = dDom;

    // mostly blind code, sorry

    // this shall be added to has.js and/or dojo or both
    has.add("dom-class-list", function(global, document, anElement) {
      return "classList" in anElement;
    });


    // new method here, but shall be included in dojo impl or used as extension
    dClass.contains1 = has("dom-class-list") ?
    function(node, classStr) {
      return dDom.byId(node).classList.contains(classStr);
      //TODO: check for border conditions in orig. dojo impl.
    } : dClass.contains;
    // just debug
    console.debug('has("dom-class-list"):', has("dom-class-list"));
    console.debug('dClass.contains1', dClass.contains1);
  });
</script>
    

Preparation code output

<div id="empty_class"> </div> <div class="a_class" id="single_class"> </div> <div class="a_class b_class c_class" id="many_classes"> </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
Add Class - Dojo
domClass.add("single_class", "some_class");
pending…
Add Class - HTML5
document.getElementById("empty_class").classList.add("some_class");
pending…
Remove Class - Dojo
domClass.remove("single_class", "a_class");
pending…
Remove Class - HTML5
document.getElementById("single_class").classList.remove("a_class");
pending…
Toggle Class - Dojo
domClass.toggle("many_classes", "a_class");
pending…
Toggle Class - HTML5
document.getElementById("many_classes").classList.toggle("a_class");
pending…
Has Class - Dojo
domClass.contains("single_class", "a_class");
pending…
Has Class - HTML5
document.getElementById("single_class").classList.contains("a_class");
pending…
classList add with dojo byId
dom.byId("empty_class").classList.add("some_class");
pending…
classList remove with dojo byId
dom.byId("single_class").classList.remove("a_class");
pending…
classList toggle with dojo byId
dom.byId("many_classes").classList.toggle("a_class");
pending…
classList contains with dojo byId
dom.byId("single_class").classList.contains("a_class");
pending…
Has Class - Dojo - with feature detection
domClass.contains1("single_class", "a_class");
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