News for hackers. Stuff that matters.
Re:Writing a Non-Blocking JavaScript Quicksort by tantalor on Wednesday June 10, 2015 @12:59AM (#9690143)
The obvious solution is to use a Web Worker,

  var code = "onmessage = function (evt) {evt.data.sort(); postMessage(evt.data)}";

  function asyncSort(data, cb) {
    var worker = new Worker(URL.createObjectURL(new Blob([code])));
    worker.onmessage = function (evt) { cb(evt.data); };
    worker.postMessage(data);
  }
Example,

  asyncSort([3, 2, 1], function (res) { console.log(res); });
Prints,

  "[1, 2, 3]"
Re:Writing a Non-Blocking JavaScript Quicksort by nemothekid on Wednesday June 10, 2015 @01:13AM (#9690193)
I thought that as well, if you are going to use setImmediate, it would probably make the most sense to just a web worker which is actually standardized and supported in all browsers (and won't turn your quick sort fn into a mess).

However it looked like he needed to support IE8, so this might be an ok solution (although I would argue a terrible one for anyone who doesn't need to support IE8/9).

Re:Writing a Non-Blocking JavaScript Quicksort by amelius on Wednesday June 10, 2015 @04:22AM (#9690651)
It is not efficient to use a webworker when transferring the data into it and out of it takes O(N) time.
Re:Writing a Non-Blocking JavaScript Quicksort by ougawouga on Wednesday June 10, 2015 @05:33AM (#9690833)
Not with transferable objects.
Re:Writing a Non-Blocking JavaScript Quicksort by amelius on Wednesday June 10, 2015 @05:39AM (#9690854)
From [1]:

> when transferring an ArrayBuffer from your main app to Worker, the original ArrayBuffer is cleared and no longer usable

This is really too restrictive in many situations.

[1] https://developers.google.com/web/updates/2011/12/Transferab...

Re:Writing a Non-Blocking JavaScript Quicksort by RandomBK on Wednesday June 10, 2015 @09:40AM (#9692047)
That may be what the API requires, but is it actually implemented as a physical move/copy of memory? It sounds like they can just make the array _appear_ to be cleared, while keeping it in place in memory.
Re:Writing a Non-Blocking JavaScript Quicksort by yoklov on Wednesday June 10, 2015 @09:54AM (#9692150)
The way I understand it is that they usually just move the pointer to the ArrayBuffer's underlying data store. No block copy occurs.
Re:Writing a Non-Blocking JavaScript Quicksort by apaprocki on Wednesday June 10, 2015 @10:53AM (#9692622)
The new SharedArrayBuffer should solve that particular problem. Once videos are online, you should check out @naveedi's talk from JSConf 2 weeks ago.
Re:Writing a Non-Blocking JavaScript Quicksort by lancefisher on Wednesday June 10, 2015 @02:45AM (#9690423)
This is a good approach for the times you can't use web workers. It's not just old browsers either. You can't draw to the canvas with a web worker.

Depending on what you're doing, you may want to yield after x operations rather than on every recursive invocation like the example shown. There is a performance cost to using setImmediate.

Re:Writing a Non-Blocking JavaScript Quicksort by yoklov on Wednesday June 10, 2015 @09:56AM (#9692169)
It's massively unfortunate that a tradeoff like this has to be made (speed vs responsiveness).

But FWIW, you can do canvas pixel operations with a web worker, which you then blit to the canvas, which is acceptable in some cases.

Re:Writing a Non-Blocking JavaScript Quicksort by error54 on Wednesday June 10, 2015 @01:18AM (#9690205)
The whole thing issue was that it had to run in IE8.
Re:Writing a Non-Blocking JavaScript Quicksort by Kiro on Wednesday June 10, 2015 @01:28AM (#9690245)
setImmediate is not supported in IE8 either.
Re:Writing a Non-Blocking JavaScript Quicksort by exogen on Wednesday June 10, 2015 @02:00AM (#9690325)
The polyfill implements setImmediate using postMessage in IE8, though. AFAIK it's not really possible to polyfill WebWorkers.
Re:Writing a Non-Blocking JavaScript Quicksort by thomasfoster96 on Wednesday June 10, 2015 @04:10AM (#9690623)
You can sort of polyfill web workers by providing the same API but running the web worker in the main thread.

Edit: I assume I'm being downvoted because you're still running the script in the main thread, and therefore lose the concurrency. Obviously this is an issue with a polyfill, but the point of polyfilling web workers would be to keep the one codebase rather than writing your your web app with and then without web workers.

Re:Writing a Non-Blocking JavaScript Quicksort by AgentME on Wednesday June 10, 2015 @04:32AM (#9690675)
Which misses the point entirely. The problem the author had was doing sorting of many items in IE8 without a warning. Using a real web worker would be a solution, but using a web worker polyfill that ran in the main thread would put him right back where he started, with IE8 triggering a warning.
Re:Writing a Non-Blocking JavaScript Quicksort by thomasfoster96 on Wednesday June 10, 2015 @05:41AM (#9690860)
I wasn't clear enough: I'd be polyfilling web workers not for this specific problem, but for other cases where I could use web workers and not lose IE8 support.