News for hackers. Stuff that matters.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by millstone on Sunday May 31, 2015 @02:37PM (#9635044)
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?
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by Drakim on Sunday May 31, 2015 @02:55PM (#9635129)
Fortunately, Transferable Objects (which didn't arrive until a bit after Web Workers) solves this :)

http://updates.html5rocks.com/2011/12/Transferable-Objects-L...

Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by rrss1122 on Sunday May 31, 2015 @10:15PM (#9636476)
Looks like the OP's linked example does not use this, since the sort reported that it took longer when using web workers than when it didn't use web workers, at least a full second longer.

I wonder how much performance would improve if the example did use Transferable Objects.

Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by angersock on Sunday May 31, 2015 @07:28PM (#9636021)
Note that IE can have some issues with transferable objects.

Also note that the messages cannot encapsulate, say, closures or anything else that you can't JSON.stringify()/JSON.parse().

Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by amelius on Sunday May 31, 2015 @05:53PM (#9635714)
If one wants to share data with two different web-workers (doing different computations on the same immutable data), there is still a problem. Or so I suppose (?)
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by Drakim on Monday June 01, 2015 @08:15AM (#9637975)
Yeah, you'd have to clone the data.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by afshinmeh on Sunday May 31, 2015 @02:58PM (#9635136)
I didn't know this, awesome.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by bgirard on Monday June 01, 2015 @03:07AM (#9637098)
You vastly overestimate the cost of copying 50k elements. On low end mobile you'll get at least 1GB/sec and on desktop you can get easily 10GB/sec. Now sorting at these speeds is a whole other story.

The real thing to consider is if the latency of the round trip messaging is less than just doing the work in place. With something like web workers you're going to have to hit the event loops when messaging back and forth which aren't always empty.

Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by afshinmeh on Sunday May 31, 2015 @02:54PM (#9635123)
Well, its just a demonstration to show that a huge cpu-bound task can be done inside the web browser. To be honest, I'm not sure if it worth to use it in production.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by rrss1122 on Sunday May 31, 2015 @10:16PM (#9636479)
I would say not having that UI lag while doing a huge cpu-bound task would be worth it in production.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by CyberDildonics on Sunday May 31, 2015 @05:26PM (#9635627)
It is still a huge win since copying is both O(N) and cache coherent, while sorting is of course O(N log(N))