News for hackers. Stuff that matters.
Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by zamalek on Friday July 31, 2015 @08:03AM (#9981207)
> For each message to be sent to or received from a worker we need to convert it to a string.

Strange, according to MDN data sent to web workers needs only be deep clone-able[1]. Is this non-standard Gecko behavior?

> aMessage: This may be any value or JavaScript object handled by the structured clone algorithm, which includes cyclical references.

[1]: https://developer.mozilla.org/en-US/docs/Web/API/Worker/post...

Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by jwmerrill on Friday July 31, 2015 @12:06PM (#9982535)
Using the structured clone algorithm for worker postMessage calls is standard:

https://html.spec.whatwg.org/multipage/workers.html#communic...

Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by msoad on Friday July 31, 2015 @02:03PM (#9983268)
It converts it to a string anyway.
Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by zamalek on Saturday August 01, 2015 @06:07AM (#9986525)
Gecko, at the very least, maintains cycles in the object graph. This is something that a naïve JSON round-trip can't do.
Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by fsdffs on Friday July 31, 2015 @04:53AM (#9980654)
If you add a wrapper, maybe use a simple heuristic to switch between JSON.parse and fetch. Even something as simple as checking the length of the string may cut it: lower than X? use JSON.parse . Higher? Use fetch.
Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by nallerooth on Friday July 31, 2015 @07:20AM (#9981114)
Sounds reasonable. This might, however, differ between different browsers too.
Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by jwmerrill on Friday July 31, 2015 @09:20AM (#9981534)
This doesn't actually succeed (yet?) at avoiding blocking the UI thread while parsing large chunks of JSON. The async API means that browsers could parse JSON from fetch responses on a different thread, but it doesn't mean that they do.

Here's a jsbin experiment that shows that the main thread is blocked while parsing JSON using the author's technique (tested in Chrome 44 and Firefox 41):

http://jsbin.com/yutahe/8/edit?js,output

Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by davej on Friday July 31, 2015 @05:58AM (#9980853)
My results on Chrome 43 running on an i7 Macbook Pro. JSON generated from: http://beta.json-generator.com/NJG1eN49

~34KB json string (1st run):

    sync: total time (blocking): 0.412ms
    async: blocking time: 1.822ms
    async: total time: 3.943ms
    ➜ str.length
    34527
~34KB json string (10th run):

    sync: total time (blocking): 0.569ms
    async: blocking time: 0.438ms
    async: total time: 2.336ms
~112KB json string (1st run):

    sync: total time (blocking): 1.217ms
    async: blocking time: 1.036ms
    async: total time: 4.818ms
    ➜ str.length
    114745
~112KB json string (10th run):

    sync: total time (blocking): 1.658ms
    async: blocking time: 0.683ms
    async: total time: 4.444ms
Could do with some proper benchmarking by someone who knows more about benchmarking this stuff for real-world usage (i.e.. not me). By the way the 2nd - 10th run results were pretty similar, the amount of time that async function was blocking gradually decreased as it was run more often (I ran the ~34KB tests first).
Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by m1el on Friday July 31, 2015 @03:49PM (#9983956)
Maybe it would be better to create a streaming message passing API. e.g. send an object in smaller parts instead of passing JSON and then sending it to the worker and fetch it in parts.
Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by efxzsh on Friday July 31, 2015 @08:58AM (#9981417)
Hmm, ok but why not

function asyncParse(string) { return new Promise(function(res, rej) { resolve(JSON.parse(string)); }); }

- or -

let asyncParse = async (string) => JSON.parse(string);

Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by alt_ on Friday July 31, 2015 @09:03AM (#9981439)
Promises still run in the main thread and will block other javascript. The purpose of this hack is to move the parsing out of the main thread.
Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by efxzsh on Friday July 31, 2015 @09:12AM (#9981479)
Ok, get it. Thx
Re:Non-Blocking Asynchronous JSON.parse Using the Fetch API by jchomali on Friday July 31, 2015 @05:57AM (#9980847)
I totally agree with this