textContent vs createTextNode() vs innerHTML

JavaScript performance comparison

Revision 30 of this test case created by Karl Seamon

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="div"></div>
<script>
Benchmark.prototype.setup = function() {
    var div = document.getElementById('div');
    var jDiv = $(div);
};

Benchmark.prototype.teardown = function() {
    document.getElementById('div').innerHTML = '';
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
innerText
div.innerText = '1234';
div.innerText = 'abc';
pending…
innerHTML
div.innerHTML = '1234';
div.innerHTML = 'abc';
pending…
textContent
div.textContent = '1234';
div.textContent = 'abc';
pending…
jQuery.text
jDiv.text('1234');
jDiv.text('abc');
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