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:
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?