根据 chrome 分析器 handleEvent 占用了大约 90% 的时间,当我执行 div_el.innerHTML = "html with hundreds of simple div"时。 handleEvent 在做什么?我使用 jquery。
最佳答案
div_el.innerHTML = html;
数百个简单的 div 由于写入 DOM 的次数而需要很长时间
- 与“离线”组装它们(作为一个简单的字符串等)并将它们全部写入 DOM 相比,这非常慢。
看看http://developer.yahoo.com/performance/rules.html
从上面(在最小化 DOM 访问 下):
Accessing DOM elements with JavaScript is slow so in order to have a more responsive page, you should:
- Cache references to accessed elements
- Update nodes "offline" and then add them to the tree
- Avoid fixing layout with JavaScript
关于javascript - 为什么 handleEvent 在插入 div 时要花这么多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4291036/