Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript code for making my browser slow down

I'm writing a library for WebWorkers, and I want to test the difference between running a script in the main page thread, versus in one or more workers. The problem is: I can't find out of hand a short function which will strain my browser enough that I can observe the difference.

A quick search didn't return much, but it might just be that I don't really know what to search for; usually I try to optimise my code, not make it slower...

I'm looking for algorithms or patterns that can be easily implemented in pure Javascript, that do not depend on the DOM or XHR, and which can have an argument passed to limit or specify how far the calculation goes (no infinite algorithms); 1s < avg time < 10s.

Extra points if it can be built without recursion and if it does not incur a significant memory hog while still being as processor intensive as possible.

like image 854
Félix Saparelli Avatar asked Oct 20 '11 18:10

Félix Saparelli


People also ask

How do I slow down my browser?

Press Control+Shift+J or Command+Option+J (Mac) Navigate to the Network Tab and open the Throttling dropdown which is set to Online by default. From the dropdown menu highlighted (as shown in the image above), select Slow 3G.

Does JavaScript make your website slow?

Poorly written JavaScript code can slow your website, negatively affecting load times and rendering speed.

How do I slow down Google Chrome?

Although hardware acceleration is supposed to make your computer and browser work better, it can slow the loading speed of web pages. You can disable hardware acceleration in your Chrome browser to make your browser faster. To do this, start by clicking on the three dots on your Chrome toolbar to open the menu.


1 Answers

Try using the obvious (and bad) recursive implementation for the Fibonacci sequence:

function fib(x) {
  if (x <= 0) return 0;
  if (x == 1) return 1;
  return fib(x-1) + fib(x-2);
}

Calling it with values of ~30 to ~35 (depending entirely on your system) should produce good "slow down" times in the range you seek. The call stack shouldn't get very deep and the algorithm is something like O(2^n).

like image 62
maerics Avatar answered Oct 19 '22 07:10

maerics