News for hackers. Stuff that matters.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by meerita on Sunday May 31, 2015 @03:08PM (#9635158)
I did all the tests faster without webworkers than with them.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by onion2k on Sunday May 31, 2015 @03:17PM (#9635185)
They're not faster. They take roughly the same amount of time, but with an extra bit of overhead for instantiating the worker and an event to say it's finished, so they're actually slower. But.. they don't block the browser doing things so the page still responses to input while they're working. If you need to do something that's doing to take 8s (e.g. this demo on my Mac Air), having the user still able to interact with the page while a progress bar updates is a huge advantage.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by afshinmeh on Sunday May 31, 2015 @03:46PM (#9635290)
Nice explanation.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by eueueu on Sunday May 31, 2015 @04:21PM (#9635390)
It's trivial to periodically pass control back to the UI with setTimeout or setInterval.

So doing something that takes 8s doesn't have to block the UI at all. The "without webworkers" version should be renamed to "Naively programmed version" or something.

Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by magicalist on Sunday May 31, 2015 @04:39PM (#9635451)
It gets ugly fast. Most tasks are not a simple sort. There's a reason that cooperative multitasking is not widely used in operating systems anymore.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by eueueu on Sunday May 31, 2015 @05:58PM (#9635733)
Javascript is not an operating system.

There aren't many operations in javascript which will block. The only real issue is that your javascript code executes in the same thread as UI. Which just means you need to think a bit harder about keeping it responsive.

"Start threads!" in programming is usually a lazy hack.

Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by LnxPrgr3 on Sunday May 31, 2015 @07:16PM (#9635981)
Lazy hack, or using OS/browser/whatever features instead of reinventing them poorly?

What's the benefit you get from programming like that?

Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by eueueu on Monday June 01, 2015 @10:37AM (#9638855)
Because once you introduce threads, your program complexity increases massively. You're now throwing a few groups of instructions at the CPU and saying "Hay! Execute these instructions in any order you like". Then you're adding code to deal with the issues of that random execution entails.

Also, it's not faster, so what's the actual point?

The browser should use all CPU cores efficiently to make javascript fast. Exposing WebWorkers to javascript programmers IMHO is totally the wrong way to do that.

Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by onion2k on Sunday May 31, 2015 @04:40PM (#9635453)
In the case of something as simple as a bubble sort, you probably could code something to periodically hand control back to the browser to pick up input events, but I imagine that would slow it down considerably as you'd need to check every few milliseconds otherwise the user will see some lag. Also, in something more complex inserting breaks would be a lot harder. Why bother when web workers are a good, working solution?
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by eueueu on Sunday May 31, 2015 @05:27PM (#9635631)
I'd bet using setTimeout with a 0ms delay every loop wouldn't slow the example down significantly, and would have the desired effect of making the UI responsive.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by wging on Sunday May 31, 2015 @05:44PM (#9635685)
You might be surprised. There are conditions under which browsers will impose a 4ms minimum delay when you ask for less. I believe it's when you set a timeout within a timeout.

Edit: close. https://html.spec.whatwg.org/multipage/webappapis.html#timer...

"Timers can be nested; after five such nested timers, however, the interval is forced to be at least four milliseconds."

Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by tedunangst on Sunday May 31, 2015 @04:32PM (#9635430)
Does that work?
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by eueueu on Sunday May 31, 2015 @05:26PM (#9635624)
Yes it does. I'm not convinced multithreading is something that should be encouraged within javascript personally.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by theCricketer on Sunday May 31, 2015 @03:14PM (#9635177)
I think the purpose was just to show that you can perform the computation without stalling the rendering thread, not that it is faster.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by afshinmeh on Sunday May 31, 2015 @03:52PM (#9635306)
Exactly. It's not necessarily faster. The point is, I wanted to show that a cpu-bound task like sorting can be done in web browser without experiencing interrupts or hiccups.
Re:Demonstrating Web Worker: Sorting a 50K array in your bro... by Ravengenocide on Sunday May 31, 2015 @03:49PM (#9635296)
> Your browser sorted 50K Array without any crash or lagging, because your browser supports Web Worker. When you do a job with Web Worker, it's just like when you run a program in another thread. Also you can see the animated progress bar while sorting. The text clearly explains that it's about not lagging or crashing, not about being faster.