innerHTML vs jQuery vs createElement() and appendChild()
JavaScript performance comparison
Info
Compares the performance of adding elements to the DOM by using strings and innerHTML, creating the DOMElement node tree and appending with appendChild(), and using jQuery to replace content with $elm.html().
Preparation code
<!-- Existing DOM elements into which the tests will append new content -->
<div id="parent-inject">Replace me via <code>elm.innerHTML()</code></div>
<hr/>
<div id="parent-create">Replace me via <code>elm.appendChild()</code></div>
<hr/>
<div id="parent-jquery">Replace me via <code>$elm.html()</code></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
/***
* Create a realistic, level playing field by storing references to container
* elements beforehand, as you would likely do in a real webapp.
***/
// Existing DOM elements into which the tests will append new content
var parentInject = document.getElementById('parent-inject');
var parentCreate = document.getElementById('parent-create');
var $parentJQuery = $('#parent-jquery');
// This content string is assigned to a variable in each loop of each test.
var contentString1 = '<div class="test" onmousemove="myFunction('
var contentString2 = ')"><h1>This is a test</h1><p>This is only a test</p></div>';
};
</script>
Preparation code output
Replace me via
elm.innerHTML()Replace me via
elm.appendChild()Replace me via
$elm.html()Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
innerHTML |
|
pending… |
createElement() and appendChild() |
|
pending… |
jQuery.fn.html() |
|
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:
- Revision 1: published by GarciaWebDev
- Revision 2: published by GarciaWebDev
- Revision 3: published by jbdemonte
- Revision 4: published by Ian White
- Revision 5: published and last updated
- Revision 6: published
- Revision 7: published
- Revision 8: published
- Revision 9: published
- Revision 10: published
- Revision 11: published
- Revision 12: published
- Revision 13: published by Ben Rolfe
- Revision 14: published
0 comments