0

I am trying to simulate slow browser performance by using web workers. I have seen a similar question about throttling JavaScript performance here, but it was asked many years ago, and without any reference to Web Workers.

I can create a web worker on the fly, and have it run an infinite loop. Therefore, I can take up CPU without blocking JavaScript in the actual tab.

Here is how I am doing this:

var workerCount = 0;

function addWorker() {
    var blob = new Blob(
        ['while (true) {}'],
        {type: "text/javascript"}
    );
    var worker = new Worker(window.URL.createObjectURL(blob));
    return 'Worker Count: ' + ++workerCount;
}

Then, I can call it from the console in that particular tab:

> addWorker();
< "Worker Count: 1"

I can see this action take up the CPU for that tab as I add workers. The fan on my computer even gets going pretty crazy. The following shows the CPU usage for 3 workers added to the page:

CPU usage is high for 3 web workers at once

As stated before, the helpful thing about using the web workers is that I can still execute JavaScript in that tab without it being blocked. I can run the following in the same tab even with the 3 workers running and it executes immediately:

for (var i = 0; i < 1000; i++) {
    console.log(i);
}

What I want to achieve:

I am trying to simulate an environment where maybe animations become more "choppy", .show(), .hide() functions take a moment to activate, etc.

While I can see the CPU usage being taken up, will this actually help simulate an environment of poor JavaScript performance in that tab? Should I maybe take up the CPU and more memory instead?

Community
  • 1
  • 1
KevBot
  • 17,900
  • 5
  • 50
  • 68
  • What is the base case for _"poor Javascript performance"_? What are you trying to achieve? – guest271314 Jul 01 '16 at 17:32
  • Can you thread javascript? if not I would say no, however I do not really know. – Adam Buchanan Smith Jul 01 '16 at 17:36
  • LOL, I just looked it up to see and found this http://www.htmlgoodies.com/html5/tutorials/introducing-html-5-web-workers-bringing-multi-threading-to-javascript.html#fbid=lHB31T8KlwH – Adam Buchanan Smith Jul 01 '16 at 17:37
  • i don't think workers would be a good way to slow down the main thread, they are designed to be apart... – dandavis Jul 01 '16 at 17:38
  • @dandavis, possibly, that's what I'm hoping to find out. From what I see in the activity monitor, web workers effect the CPU usage for the tab, which is where the normal page JavaScript will be executing so I would assume this would have an overall performance impact. – KevBot Jul 01 '16 at 17:41
  • _"I am trying to simulate an environment where maybe animations become more "choppy""_ Not following, here. Can you create a stacksnippets to demonstrate where animatations become "choppy"? Not certain what requirement is? – guest271314 Jul 01 '16 at 17:41
  • set a short interval running a small while loop to cause main thread chop. – dandavis Jul 01 '16 at 17:44
  • Why not just go with a VM as indicated in the linked post? Browser makers try very hard to make animation *not* choppy, and you're trying to undermine that. Also, I believe some browsers use the GPU for animation, so you might be out of luck. – Heretic Monkey Jul 01 '16 at 17:45
  • that reminds me, you can change some chrome flags to kill optimizations and accelerations, and the devtools has a network throttle. – dandavis Jul 01 '16 at 18:00
  • Just as others, I wanna advise that you use virtual machine. Method with web workers is unreliable - you never know what is actually going on, how is CPU load getting deferred etc... – Tomáš Zato Jul 04 '16 at 14:18

0 Answers0