在javascript中构建一个大而复杂的dom-subtree是相当缓慢和昂贵的。有没有可能在后台构建这样一棵树,也许使用worker,然后在构建完成后才将其附加到主dom树上?
我知道工作人员无法访问dom和xml库,但也许有一种常见的解决方法可以在需要时延迟加载子树。
var c = document.createDocumentFragment();
// takes several seconds to execute, should not block main thread.
// may be inside a worker...
buildLargeAndComplexSubtree(c);
// append subtree to the main tree in the main thread.
document.body.appendChild(c);发布于 2018-06-15 07:57:44
也许使用setTimeout将有助于将其从主线程中移除并提高性能:
setTimeout(function () {
var c = document.createDocumentFragment();
// takes several seconds to execute, should not block main thread.
// may be inside a worker...
buildLargeAndComplexSubtree(c);
// append subtree to the main tree in the main thread.
document.body.appendChild(c);
}, 0);
https://stackoverflow.com/questions/50867166
复制相似问题