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.
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.
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.
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)
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.
Be more confident in browsers’ capabilities.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With