I'm working with Isolates right now and wanted to know if using IndexedDB to share data between Isolates is a good way to communicate? Specifically, I want one Isolate to be able to write to it, then tell the other Isolates they may readonly it. This is data that would be considered unchanging once it is written and is fairly large. The main reason I want to do this is because I don't want to send a 6MB Map to 3 different Isolates since it is a bit intensive on the program.
2 Answers
Web workers allow you to pass array buffers between workers without copying. However once you pass an array buffer to another worker the original worker looses access to its contents.
So you could try store your data structure in an array buffer to take advantage of this.
See this mdn article, and html5rocks.
So for Dart, store your data in a ByteData, and then when compiled via dartjs this should not be copied, but transferred.
Side note: Mozilla is also doing some experimentation with shared mutable access to array buffers across multiple web workers. This is to allow emscripten to compile multithreaded c code to javascript. This sort of shared access will likely take a while to get standardised and widely implemented.

- 15,430
- 2
- 30
- 33
-
I haven't looked into it yet, but if the reference is lost, I can't send the same data to multiple web workers without making copies? – Austin Salgat Feb 17 '14 at 00:37
-
Correct. It is not possible to use the data in multiple web workers simultaneously without copying it. Isn't the async indexedb api, the one available to workers, basically just message passing with the data stored in a single thread/isolate/worker anyway, not sure why you see indexedb as a work around for this, as it's going to have the same limitations as far as I can see. – Greg Lowe Feb 17 '14 at 01:06
-
I was hoping readOnly transactions resulted in a single reference being sent to whatever requested the data. – Austin Salgat Feb 17 '14 at 03:44
If you want to store the data in the IndexedDB anyway this is fine. If you do it just to optimize the communication I don't think this is an improvement over sending directly. Serialization/deserialization is usually the most CPU intensive part. Using IndexedDB additionally writes once and reads three times to/from the slow disk.

- 623,577
- 216
- 2,003
- 1,567
-
You don't think there'd be caching going on to improve performance? My main quip is that just sending between web workers means that the program has to do a deepCopy on a huge chunk of data (6MB Map in my case) before sending it to each thread, versus just reading from the same cached reference. – Austin Salgat Feb 15 '14 at 18:10
-
1Didn't think of that. I would benchmark it to see what performs better. – Günter Zöchbauer Feb 15 '14 at 19:54