gpt4 book ai didi

javascript - 从 DOM 中删除元素对性能有何影响?

转载 作者:行者123 更新时间:2023-12-02 17:53:40 27 4
gpt4 key购买 nike

我有一个动画循环,它向 DOM 添加一个新元素并为其设置动画,这基本上可以永远重复。我的问题是,当每个动画周期完成后,从 DOM 中删除该元素还是将其隐藏在页面上更好?我无法重用该元素,因为循环的工作方式可能会在另一个动画完成时开始一个新动画,因此在给定时间页面上可能有多个元素。我意识到这个问题相当基本,但希望能得到一些见解。谢谢。

最佳答案

Performance implications

几乎没有。然而,这会带来内存影响——用(隐藏的)元素填充 DOM 并且不停地这样做是邪恶的。当然,在某些时候这会减慢整个过程。

is it better to remove the element from the DOM or to just leave it hidden on the page?

一定要删除它。

I can't reuse the element since the way the loop works, a new animation may begin while the other one is finishing, so there could be multiple elements on the page at a given time.

您仍然可以通过维护元素池来重用它们,但这可能没有必要。删除旧的并创建新的就可以了。

关于javascript - 从 DOM 中删除元素对性能有何影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21145495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com