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
Which method to remove all content from an element is fastest?
Updated 2013-12-13: make sure that there actually are elements to be deleted; jsperf setup does NOT run before each test, but before each test LOOP!!
<div id="box" style="display:none;"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>
var fragment = document.createDocumentFragment();
for (var i=0;i<1000;i++){
var c = document.createElement('div');
c.textContent = 'siodfj'+Date.now();
fragment .appendChild(c);
}
function clearChildren(el) {
var child;
while(child=el.firstChild)
el.removeChild(child);
}
var box = document.getElementById('box');
function reset(ignore) {
if (!ignore && box.childNodes.length > 0) throw 'child nodes not cleared!';
box.appendChild(fragment.cloneNode());
}
var slice = Array.prototype.slice;
</script>reset(true);Ready to run.
| Test | Ops/sec | |
|---|---|---|
| removeChild reversed | | ready |
| innerHTML | | ready |
| jQuery empty | | ready |
| remove firstChild | | ready |
| remove lastChild | | ready |
| remove firstChild function | | ready |
| remove firstChild with var | | ready |
| remove firstChild with null check | | ready |
| remove lastChild with var | | ready |
| textContent | | ready |
| empty fragment | | ready |
| forEach | | ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.