I am working on Swagger Editor performance. One of the solutions to speed things up was moving process-intensive task to Web Workers. Web Workers do a great job of moving process-heavy tasks out of the main thread but the way we can communicate with them is very slow. For each message to be sent to or received from a worker we need to convert it to a string. This means for transferring objects between the main thread and worker threads we need to
JSON.stringify our objects back and forth.
For larger objects, this can lead to large blocking
JSON.parse calls. For example, when transferring back the AST from our AST-composer worker I saw a
50ms pause. A 50 millisecond pause can easily drop 4 frames.
JSON.parse in a worker is also pointless.
We can use (abuse?) this API to move all of our JSON-parsing business out of the main thread. It can be done as simple as:
It works as expected:
JSON.parse out of the main thread make the actual parsing time less important but let’s see how it’s different than native
The async method is about 2x slower but hey, it’s async and using it blocked the UI for less than a millisecond!
async/await in ES7(ES2016) working with async methods are much easier so we should have async JSON APIs as well.