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?
jsperf setup does not run before each test, but before each test loop. We reset()
the element after each iteration to (a) throw an exception if it isn't empty, and (b) add the same child elements back to it for the next iteration.
<div id="box" style="display:none;"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
var box = document.getElementById('box');
var fragment = document.createDocumentFragment();
for (var i = 0; i < 500; i++) {
var c = document.createElement('div');
c.textContent = 'asdf' + Date.now();
fragment.appendChild(c);
}
function reset(ignore) {
if (!ignore && box.childNodes.length > 0) throw 'child nodes not cleared!';
box.appendChild(fragment.cloneNode(true));
}
var slice = Array.prototype.slice;
</script>
reset(true);
Ready to run.
Test | Ops/sec | |
---|---|---|
for-reverse removeChild |
| ready |
while firstChild removeChild firstChild |
| ready |
while firstChild removeChild lastChild |
| ready |
while lastChild removeChild firstChild |
| ready |
while lastChild removeChild lastChild |
| ready |
while var firstChild removeChild var |
| ready |
while var lastChild removeChild var |
| ready |
while not-null firstChild removeChild firstChild |
| ready |
while not-null firstChild removeChild lastChild |
| ready |
while not-null lastChild removeChild firstChild |
| ready |
while not-null lastChild removeChild lastChild |
| ready |
while hasChildNodes removeChild firstChild |
| ready |
while hasChildNodes removeChild lastChild |
| ready |
textContent |
| ready |
innerHTML |
| ready |
slice call forEach |
| ready |
range selectNodeContents deleteContents |
| ready |
replaceChildren |
| ready |
$ empty |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.