Advertisement
NikolaDimitroff

DOM is leaky

Oct 14th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.01 KB | None | 0 0
  1. <!HTML DOCTYPE>
  2. <html>
  3. <head>
  4.     <style>
  5.         div {
  6.             width: 30px;
  7.             height: 30px;
  8.             border: 1px solid black;
  9.         }
  10.     </style>
  11. </head>
  12. <body>
  13.  
  14.     <script>
  15.     const shouldAppendAllAtOnce = true;
  16.     const totalDivCount = 300;
  17.    
  18.     function appendOneAtATime(count) {
  19.         for (let i = 0; i < count; i++) {
  20.            document.body.innerHTML += '<div></div>';
  21.         }
  22.     }
  23.     function appendAll(count) {
  24.         let fragment = document.createDocumentFragment();
  25.         for (let i = 0; i < count; i++) {
  26.            let el = document.createElement('div');
  27.            fragment.appendChild(el);
  28.        }
  29.        document.body.appendChild(fragment);
  30.    }
  31.    const startTime = Date.now();
  32.    if (shouldAppendAllAtOnce) {
  33.        appendAll(totalDivCount);
  34.    } else {
  35.        appendOneAtATime(totalDivCount);
  36.    }
  37.    const duration = Date.now() - startTime;
  38.    console.log('Total time taken: ', duration);
  39.    
  40.    </script>
  41. </body>
  42. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement