creating text nodes

JavaScript performance comparison

Revision 3 of this test case created by Marcin

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var textContentProperty = document.body.textContent ? 'textContent' : 'innerText';
  
  function testAndRemove(DIV) {
    if (DIV.innerHTML !== '&nbsp;') {
      throw new Error("Text not set correctly");
    }
    document.body.removeChild(DIV);
  }
  
  function empty(DIV) {
    var child;
    while (child = DIV.lastChild) {
      DIV.removeChild(child); //clear
    }
  }

};
</script>

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
innerHTML
var DIV = document.createElement('DIV');
document.body.appendChild(DIV);
DIV.innerHTML = '&nbsp;';
DIV.innerHTML = ''; //clear
DIV.innerHTML = '&nbsp;';
testAndRemove(DIV);
pending…
createTextNode
var DIV = document.createElement('DIV');
document.body.appendChild(DIV);
DIV.appendChild(document.createTextNode('\u00A0'));
empty(DIV); //clear
DIV.appendChild(document.createTextNode('\u00A0'));
testAndRemove(DIV);
pending…
innerText/textContent
var DIV = document.createElement('DIV');
document.body.appendChild(DIV);
DIV[textContentProperty] = '\u00A0';
empty(DIV); //clear
DIV[textContentProperty] = '\u00A0';
testAndRemove(DIV);
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