text content

JavaScript performance comparison

Revision 11 of this test case created

Preparation code

<div id="text">content to be retrieved</div>
<script>
  var elem = document.getElementById("text");
  var elem2 = document.createElement('div');
  var preTextNode = document.createTextNode('Hello there');
</script>
<script>
Benchmark.prototype.setup = function() {
    elem2 = document.createElement('div');
    elem.innerHTML = 'content to be retrieved';
};

Benchmark.prototype.teardown = function() {
    elem2 = document.createElement('div');
    elem.innerHTML = 'content to be retrieved';
};
</script>

Preparation code output

content to be retrieved

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
read innerHTML
var x = elem.innerHTML;
pending…
read textContent
var x = elem.textContent;
pending…
read data
var x = elem.firstChild.data;
pending…
write innerHTML
elem.innerHTML = 'written content'
pending…
write textContent
elem.textContent = 'written content'
pending…
write data
elem.firstChild.data = 'written content'
pending…
write data w/ check
if (elem.firstChild) elem.firstChild.data = 'written content';
pending…
write data w/ check 2
if (elem2.firstChild)
  elem2.firstChild.data = 'written content';
else
  elem2.appendChild(document.createTextNode('written content'));
pending…
write data w/o check 2
elem2.appendChild(document.createTextNode('written content'));
pending…
write data w/o check 2b
elem2.appendChild(preTextNode);
pending…
write data w/o check 3b
elem.replaceChild(preTextNode,elem.firstChild);
pending…
write data w/ check 2+
if (elem2.firstChild)
  elem2.firstChild.data = preTextNode.data;
else
  elem2.appendChild(preTextNode);
pending…
replace w/ string
var tmpNode = document.createTextNode('written content');
elem.replaceChild(tmpNode,elem.firstChild);
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