Is there a way to create out of DOM elements in Web Worker?

后端 未结 9 878
一个人的身影
一个人的身影 2020-11-30 02:58

Context: I have a web application that processes and shows huge log files. They\'re usually only about 100k lines long, but it can be up to 4 million lines

9条回答
  •  星月不相逢
    2020-11-30 03:27

    So is there any way, that I'm not aware of, to create DOM elements, outside the DOM tree, in a Web Worker?

    No.

    Why not? It seems to me that this can't create concurrency problems, as creating the elements could happen in parallel without problems.

    Not for creating them, you're right. But for appending them to the main document - they would need to be sent to a different memory (like it's possible for blobs) so that they're inaccessible from the worker thereafter. However, there's absolutely no Document handling available in WebWorkers.

    I create a DOM element for each line as soon as the data arrives (in JSON via ajax). Afterwards I save the elements in an array and I only show the lines that are visible.

    Constructing over 500k DOM elements is the heavy task. Try to create DOM elements only for the lines that are visible. To improve performance and showing the first few lines faster, you also might chunk their processing into smaller units and use timeouts in between. See How to stop intense Javascript loop from freezing the browser

提交回复
热议问题