jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
An attempt at a realistic simulation.
Assume a set of 400 nodes and there is some change requiring 150 nodes to be replaced.
What is the most efficient way to do that?
InnerHTML test does a complete replacement of all 400 nodes because thats all innerHTML can do, whilst the other tests only change the 150 nodes, the Control test does the same thing on all 400 nodes using remove/appendChild to help better understand the relative performance of innerHTML vs node manipulation. InnerHTML + Clone look to a faster way to replace all nodes.
Conclusion is that although innerHTML is more performant than node manipulation when replacing all child nodes, node manipulation often allows you to do less work and is therefore faster in removing a subset of nodes, noting that even so bulk inserting nodes via insertAdjacentHTML is faster than appending nodes.
The Update tests look to simply update the content of a node instead of removing and then appending it, thereby saving creating new dom nodes. Both test are slower!
<div id="container"></div>
<style type="text/css">
#box, #box * {
font-size: 0;
}
#box * {
display: inline-block;
width: 1px;
height: 1px;
padding: 0;
margin: 0;
background-color: blue;
}
</style>
var box = document.createElement('div');
box.id = 'box';
document.getElementById('container').appendChild(box);
function refill(){
var box = document.getElementById('box');;
var html='';
var n = '<div><span>a</span><span>a</span><span>a</span><span>a</span><span>a</span><span>a</span><span>a</span><span>a</span><span>a</span><span>a</span></div>';
for(i=0;i<400;i++)html+=n;
box.innerHTML=html;
}
refill();
document.getElementById('container').removeChild(box);
Ready to run.
Test | Ops/sec | |
---|---|---|
RemoveChild |
| ready |
Range |
| ready |
InnerHTML |
| ready |
Append |
| ready |
Control |
| ready |
Update |
| ready |
Update outside dom |
| ready |
InnerHTML+Clone |
| ready |
InnerText/InnerHTML |
| ready |
Fragment |
| ready |
Fragment Partial |
| ready |
Frag Partial 400 |
| ready |
Frag 400 InnerText |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.