Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>detach() & reinsert</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style>#big{border:1px solid #ccc;padding:6px}</style> </head> <body> <div id="big"><p>A</p><p>B</p><p>C</p></div> <button id="work">Do heavy DOM work</button> <pre id="out"></pre> <script> $("#work").on("click", function(){ var t = performance.now(); var $el = $("#big").detach(); // work off-DOM for(var i=0;i<500;i++){ $el.append("<p>Row "+i+"</p>"); } $("#out").text("work took " + (performance.now()-t).toFixed(2) + "ms"); $("body").append($el); // reinsert once }); </script> </body> </html>
Output