Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Batch DOM Updates (avoid thrash)</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style>.card{border:1px solid #ccc;margin:2px;padding:4px 8px;display:inline-block}</style> </head> <body> <button id="build">Build 500 cards</button> <pre id="out"></pre> <div id="wrap"></div> <script> function time(label, fn){ var t = performance.now(); fn(); $("#out").append(label + ": " + (performance.now()-t).toFixed(2)+"ms\n"); } $("#build").on("click", function(){ $("#wrap").empty(); time("append per item", function(){ for(var i=0;i<500;i++){ $("#wrap").append('<div class="card">Item '+i+'</div>'); } }); $("#wrap").empty(); time("single append", function(){ var html=[]; for(var i=0;i<500;i++){ html.push('<div class="card">Item '+i+'</div>'); } $("#wrap").append(html.join("")); }); }); </script> </body> </html>
Output