Fastest DOM insertion

后端 未结 5 638
花落未央
花落未央 2020-12-08 22:43

What are the best practices for doing DOM insertion?

  • Is it faster to insert large chunks of html vs element at a time in a loop?
  • Does it matter what h
5条回答
  •  长情又很酷
    2020-12-08 23:10

    Setting innerHTML is often faster than inserting seperate nodes.

    Another possibility would be to create a DocumentFragment, which allows to insert the nodes all at once. Another advantage of using DocumentFragments is that they can easily be cloned, which can replace a lot of use cases for innerHTML and is potentially faster as no parsing is involved.

提交回复
热议问题