Is there a way to pass just some of a data structure to a web worker?

Friday, July 11, 2014

I have a complex data structure that basically has linked lists of elements, where some elements then have linked lists of sub-elements. Basically from any one element there are ways to traverse to any other element. The whole system works beautifully inside my web worker.

However, sometimes I need to pass a subset of this structure to the main thread using postMessage(). If I call postMessage(data) where data is any object in the structure, the entire structure will be copied across because of these linked lists (10,000 - 200,000 objects).

I can create copies of all objects that I want copied, with the copied objects only linked to other objects I want copied. But that means building 200 - 1,000 objects, that are 99% copies of existing objects, just for a single postMessage(). And we'll be posting this across a lot.

A second approach is to temporarily null some of the links. I'm not wild on this because it strikes me a hack. But it would be fast.

Is there any other way? For example, can I control what the structured clone algorithm will copy?

thanks - dave