addRule vs. insertRule (v5)

Revision 5 of this benchmark created on


Description

Tests which is faster when adding a rule to a stylesheet: insertRule or addRule.

WARNING: this may not work in all browsers. Chrome definitely has both.

NOTE: removeRule will be used in the tearDown.

Preparation HTML

<a>Test text. This text will be changed by the CSS rule.</a>

<script>
  //Create and add stylesheet to page.
  document.head.appendChild(document.createElement("style"));
  var stylesheet = 
    document.styleSheets[document.styleSheets.length-1];
  
</script>

Teardown


    stylesheet.removeRule(0);
  

Test runner

Ready to run.

Testing in
TestOps/sec
addRule Test
stylesheet.innerHTML = "a { color:#000000; }";
ready
insertRule Test
stylesheet.insertRule("a { color:#000000; }",0);
ready
textContent
stylesheet.addRule("a","color:#000000");
ready
innerText
stylesheet.insertRule("a { color:#000000; }", stylesheet.cssRules.length);
ready

Revisions

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