Is it possible to “buffer” DOM changes that happen in a loop (to increase performance)?

前端 未结 5 2025
轻奢々
轻奢々 2021-01-06 10:50

To make it clear what I\'m asking, here is my example (fiddle).

I have a list of ~500 random names. I have an input at the top that has live-style searching. On ever

5条回答
  •  梦毁少年i
    2021-01-06 11:20

    As long as you do not query properties that trigger an immediate reflow the browser already batches the updates for you and only relayouts/repaints the page once your javascript is done. You should be able to verify this in the browser devtools.

    But there is a trivial optimization for your code: avoid redundant DOM manipulations, especially for elements that are not visible anyway.

    The else branch that gets applied for hidden items:

    else {
      hideItem(item);
      toPlainText(item);
    }
    

    the toPlainText manipulates the DOM even the element will be hidden or already is hidden.

提交回复
热议问题