Web Workers and drive by CPU nabbing

Web workers are a great part of the html5 specification. They let you run a javascript program in a separate process; so that complex tasks can be done in the background without blocking nice, fluffy, user interactions with the page. Better still, if multiple web workers are run and multiple CPUs are available then they get scheduled across all the processor - multi-core javascript enables whole new avenues of complex (and awesome) code to be written and deliver a great experience.

....you can probably sense the but coming.....but - this means any page you navigate to, any link you click, any random website with funny pictures of cats on, has unfettered access to your CPU. While you browse worldsfunniestcats.com, it could be using your electricity to generate BitCoins for itself, or crack passwords.

To prove a point, below is a page that spawns a few web workers and sets them, continuously, generating prime numbers. Open up the page in a modern browser and check out your task manager (closing the page kill the web workers):

Update: It doesn't max-out the CPU in Opera...why? Because apparently Opera is so fast that it can handle the workload without breaking a sweat!

Update 2: Apparently IE10 can hack it too, looks like firefox and chrome are trailing on this..... there's a sentence I never thought I'd write.....

Update 3: On further investigation, it looks like Opera's performance is a combination of running the script at great speed AND some kind of limits on web workers, either in terms of number or CPU usage. I can't comment on IE10's performance as I don't have a windows 7 laptop..... if anyone is listening at microsoft..... *hint* *hint* :)

Everyone loves social media!
    • http://lauhakari.com Lauhakari [mikko]

      feels like the same crap that happens when a page with flash gets opened =/

    • http://www.ubelly.com Martin Beeby

      Also IE10 Platform Preview 2 seems to handle the load too.

    • http://philingrey.com Phil Ingrey

      @Martin Wow, pretty unexpected (it being the first version with web workers)! But good to know.

    • http://cksiteoftreats.co.uk CK

      Only hits 2 cores on mine (Win 7 64-bit, FF5). And how can Opera “handle the workload” – surely it should run as fast as possible?

      • http://philingrey.com Phil Ingrey

        Interesting that it only maxes out two cores…, Opera handles the workload to such an extent that the web worker’s channel communications are taking longer than the computations, and becoming the limiting factor. You just have to compare the speed at which new primes are found in opera compared with firefox to see the difference (and as a firefox fanboy – that’s painful to say!)

    • Martin

      Hmm, but Opera only uses a single core for me…
      And the reason it stays responsive is probably that it implements user level threading and thus controls the workload just like everything else…

      • http://philingrey.com Phil Ingrey

        Yep, it looks like a combination of good javascript performance and some sort of worker limiting – Increasing the number of workers doesn’t seem to impact the amount of CPU Opera takes up. I’m surprised it only uses one core, it was hitting around 65% on two cores for me.

    • Steve H

      On Windows XP, Firefox 5, dual core, it hit 99% and the user interface ceased being responsive for several seconds.

      Not happy about this!

    • http://webworkerzine.com Dan

      I want to express my appreciation to this writer just for bailing me out of such a difficulty. Because of scouting through the search engines and getting strategies which were not pleasant, I assumed my life was done. Existing without the presence of strategies to the problems you have sorted out as a result of your short article is a serious case, and ones that could have negatively affected my career if I had not noticed your site. Your own competence and kindness in dealing with a lot of stuff was useful. I am not sure what I would have done if I had not come across such a subject like this. It\\\\\\\’s possible to at this point look ahead to my future. Thanks for your time so much for your skilled and effective guide. I will not think twice to propose your blog post to any individual who ought to have direction on this subject matter.