Create element vs set HTML
JavaScript performance comparison
Preparation code
<div id="test"><div>old content</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
var existing = $('#test').children()[0];
var parent = $('#test')[0];
var renderNewElement = function() {
var e = document.createElement('div');
e.innerHTML = "<div>some content</div>";
parent.replaceChild(e, existing);
existing = e;
};
var renderInnerHTML = function() {
parent.innerHTML = "<div>some content</div>";
}
};
</script>
Preparation code output
old content
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
New element |
|
pending… |
InnerHTML |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments