Reputation: 15099
Let's say I have some big amount of data to be processed. For the sake of this example, let's say that the data just can't be processed on the server side; it must be processed on the client side. Let's also say that the entire data process looks like this:
for element in data do:
//do some work with "element"
end
Again, for the sake of this example, let's say that data
contains 100.000 elements.
Calling the process function will freeze the entire DOM until the loop has reached it's end. My question is: is there some way I could do this processing in an non-blocking way? Be it some sort of async/wait, processing chunks of data
with some type of yield
mechanism, or pretty much anything else?
EDIT: Web Workers are not an option as I need at least IE 11 support.
EDIT 2: Seems I was mixing "Web Workers" with "Shared Web Workers". Question is answered.
Upvotes: 0
Views: 285
Reputation: 17019
You could just loop a given amount of elements each time, processing the next batch in the next stack. To accomplish this you must use setTimeout
. That is quite rudimentary but works in any browser..
Maybe using reduce or something.. sending the rest to the next stack and so forth..
This article may help https://benjaminhorn.io/code/part-2-cpu-intensive-javascript-computations-without-blocking-the-single-thread/
This only can be considered the right answer if you cannot rely on web workers, since this is for what they were specified.
Upvotes: 1