Demonstrating Web Worker: Sorting a 50K array in your browser
I don't know much about web workers. According to my understanding, they can only communicate by message passing. This means that, if I have a 50K array and wish to sort it via a web worker, I must copy it twice: once incoming and again outgoing. This copying is likely to be as expensive as sorting it (well, maybe not if you use bubble sort). How do you mitigate this?
Deeply misleading title. It's basically pointing out that if you use "Web Worker" then you can do stuff in the browser in background. It's really nothing to do with sorting the array - it's just using that as a task for the background work.
Web workers would have been a must have for a project I worked in a few years ago. I had to write a real-time video allocation algorithm that had dynamic hardware with extreme combination constraints, and varying encoding rules for audio and video (think HD, SD, Dolby, stereo). It also had to allow for changes without moving existing allocations (otherwise viewers TV programmes would glitch).
The back-end was an embedded device that only accepted XML and could not do the calculation.
I had to have this do the allocation in IE8! So I wrote a timer based allocation routine that would release the UI thread enough to not lock up the browser. It worked (sort of), but was ugly and took way too long!
I think it should be more clear that it's using bubble sort to purposely take a longer time than necessary.
I'd expect sorting an array of 50k elements to be instantaneous on a modern machine, even in Javascript.
My first thought was, "WTF? Sorting 50k elements is a big deal?"
I found this thread from about 3 years ago interesting:
https://news.ycombinator.com/item?id=2566365
Basically if web workers could access the GPU as well via WebGL.
...with bubble sort.
Might want to mention that in the title.
I did all the tests faster without webworkers than with them.
Is co-operative multitasking really as far as it's got, after two decades of web progress? Sheesh.