Stylesheet vs DOM manipulation (v7)

Revision 7 of this benchmark created by Mo on


Description

Trying to benchmark what is less time consuming, DOM manipulation or adding CSS to the stylesheet through javascript.

Preparation HTML

<style type="text/css">.red{color:red}.green{color:green}</style>
<div id="untouched">This DIV is unchanged</div>
<div id="touched">This DIV needs to be manipulated</div>

Setup

var sheet = (function() {
        // Create the <style> tag
        var style = document.createElement("style");
    
        // Add a media (and/or media query) here if you'd like!
        // style.setAttribute("media", "screen")
        // style.setAttribute("media", "only screen and (max-width : 1024px)")
    
        // WebKit hack :(
        style.appendChild(document.createTextNode(""));
    
        // Add the <style> element to the page
        document.head.appendChild(style);
    
        return style.sheet;
    })();
    
    function addCSSRule(sheet, selector, rules, index) {
        if("insertRule" in sheet) {
                sheet.insertRule(selector + "{" + rules + "}", index);
        }
        else if("addRule" in sheet) {
                sheet.addRule(selector, rules, index);
        }
    }
    
    var elemPreselected = document.getElementById("touched"),
        elemPreselectedStyle = elemPreselected.style;
    
    var head=        document.getElementsByTagName('head')[0]
    function appendStyle(css) {
            var style = document.createElement("style");
            style.setAttribute('rel', 'stylesheet');
            style.setAttribute('type', 'text/css');
            
            style.innerHTML = css;
            
            head.appendChild(style);
    };
    
    
    function replaceStyleAttribute(e, css) {
          e.setAttribute("style", css);
    }
    
    function appendStyleAttribute(e, css) {
         e.setAttribute('style', e.getAttribute('style') + ";" + css);
    }

Teardown


    var elemPreselected = document.getElementById("touched");
    elemPreselected.style.color = "";
    elemPreselected.className = "";
  

Test runner

Ready to run.

Testing in
TestOps/sec
DOM Manipulation
appendStyle("#touched{ color:red; color:green; }");
ready
JS CSS Injection
addCSSRule(document.styleSheets[0], "#touched", "color: red");
addCSSRule(document.styleSheets[0], "#touched", "color: green");
ready
Add class to DOM
var elem = document.getElementById("touched");
elem.className = "red";
elem.className = "green";
ready
DOM Manipulation with preselected element
elemPreselected.style.color = "red";
elemPreselected.style.color = "green";
ready
Add class to DOM with preselected element
elemPreselected.className = "red";
elemPreselected.className = "green";
ready
DOM Manipulation rewrite style text
var elem = document.getElementById("touched");
elem.style.cssText = "color:red";
elem.style.cssText = "color:green";
ready
DOM Manipulation rewrite style text with preselected element
elemPreselected.style.cssText = "color:red";
elemPreselected.style.cssText = "color:green";
ready
DOM Manipulation with preselected element's style
elemPreselectedStyle.color = "red";
elemPreselectedStyle.color = "green";
ready
Dom manipulation
var elem = document.getElementById("touched");
elem.style.color = "red";
elem.style.color = "green";
ready
Replace style attrrbiute
replaceStyleAttribute(elemPreselected, "color:red; color:green");
ready
Append style attribute
appendStyleAttribute(elemPreselected, "color:red; color:green");
ready

Revisions

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

  • Revision 1: published by MacK on
  • Revision 2: published by qgustavor on
  • Revision 3: published by check_ca on
  • Revision 4: published by Roman Liutikov on
  • Revision 6: published by Pierre Reimertz on
  • Revision 7: published by Mo on