Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort DOM elements triggering the least amount of reflow?

I've got the following example.

<div class="parent">
  <div data-id="5"></div>
  <div data-id="2"></div>
  <div data-id="3"></div>
  <div data-id="1"></div>
  <div data-id="4"></div>
</div>

If I want to order these div's in an ascending order (1,2,3,4,5). I would normally do a loop and append the div's in order to the parent div. This will however mean I always make 5 changes to the dom (regardless of the order of the div's), one for each div.

You can however use the .insertBefore() method to order the div's correctly with only 2 changes!

5,2,3,1,4
Insert 1 before 2
5,1,2,3,4
Insert 5 behind 4 (using .insertBefore() and .nextSibling)
1,2,3,4,5 

Question 1 By making only 2 changes to the DOM I assume less reflow ocours, making the '2 change' sorting action faster than the '5 change'sorting action. Is this correct?

Question 2 What sort of method/algorithm would be able to figure out to only do the insert 1 before 2 and 5 behind 4?

Question 3 (bonus) Will this 'optimized' algorithm still be faster as the amount of items incease? Range 10 - 100 - 1.000 - 10.000 - 100.000

Maybe to clarify: I am not searching for a way to figure out the order (1,2,3,4,5) in the most optimal way. At a certain point I know the order but I want to compare the order agains the order of the div's and THEN figure out the least amount of operations.

like image 953
Dex Avatar asked Jul 26 '17 08:07

Dex


People also ask

Why does it take so long to reflow a DOM element?

Sometimes reflowing a single element in the document may require reflowing its parent elements and also any elements which follow it. Every time the DOM changes browser needs to recalculate the CSS, do a layout and repaint the web page. This is what takes time in real dom.

What is a Dom reflow?

Simply put, reflow is a user-blocking operation that computes the layout of the document. A reflow on an element is the calculation of its dimensions and position in the document. DOM reflow happens more often that you would expect.

What is a reflow in HTML?

Simply put, reflow is a user-blocking operation that computes the layout of the document. A reflow on an element is the calculation of its dimensions and position in the document. DOM reflow happens more often that you would expect. It happens when: Taking measurements of an element ( offsetHeight or getComputedStyle)

How to use the reflow spiral effect in CSS?

Due to the reflow spiral effect, it’s recommended to trigger the reflow as low in the tree as possible to minimise subsequent reflows that might get triggered on children elements. Say you want to toggle a class in order to apply a set of CSS styles, do so always on the element you want to change, not on the parent element.


1 Answers

Be more confident in browsers’ capabilities.

  1. Browsers batch together DOM operations when they are performed in one single, synchronous execution of a JavaScript sequence. Exceptions occur when you explicitely (and sometimes unknowingly) request a reflow by accessing DOM properties/methods that require the DOM to be up-to-date, e.g. innerWidth, offsetTop, or getBoundingClientRect. See Rendering: Repaint, Reflow, Restyle for details.
    Deleting DOM nodes is not necessary. Add them to a newly created DocumentFragment, they’ll be automatically removed from their current position in the DOM.

  2. Browsers, more specifically JS engines, already know and use the smartest sorting algorithm. Save for very specific cases, you don’t need to know about the inner mechanisms of a sorting operation. Just use Array.prototype.sort, provide a custom function if necessary, and watch magic happen.

like image 112
Watilin Avatar answered Nov 15 '22 07:11

Watilin