Live data from Hacker News

Writing a Non-Blocking JavaScript Quicksort

breck-mckye.com

1–10 of 43 posts

Re: Writing a Non-Blocking JavaScript Quicksort

#2
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

#3
post #2

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]"

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

#4
post #2

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]"

The whole thing issue was that it had to run in IE8.

Re: Writing a Non-Blocking JavaScript Quicksort

#6
post #4
post #2

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]"

The whole thing issue was that it had to run in IE8.

setImmediate is not supported in IE8 either.

Re: Writing a Non-Blocking JavaScript Quicksort

#7
post #6
post #4

Earlier quoted context omitted.

The whole thing issue was that it had to run in IE8.

setImmediate is not supported in IE8 either.

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

#10
post #5

The 'long tail' really oughtn't be that long if they switched to the native sort when a partition becomes small enough.

> switched to the native sort when a partition becomes small enough

Isn't that exactly what they are doing?

>> Our approach was simply to prefer the native implementation unless working in IE8 or with arrays over a thousand items.

Post reply on HN