How it works...

We've seen a simple string posted to and from workers. Now we can see that more complex objects can be sent as well. In fact, a sizable number of types can be passed via postMessage.

By using postMessage, we have copied the data to the worker. This operation is costly, because whenever the message is posted to a new thread, the data must be duplicated in order to be available. For small datasets, this isn't a problem, but for larger sets it can be slow and memory-intensive. For future recipes in this chapter, we'll be using shared memory to avoid this copying.

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.