在javascript中构建一个庞大而复杂的dom-subtree非常缓慢且昂贵。是否可以在后台构建这样的树,也许使用worker
,并且仅在建筑物完成后将其附加到主圆顶树中?
我知道工人无法访问 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);
也许使用 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);