Javascript code for making my browser slow down

Check out the benchmarking code referenced by the Google V8 Javascript Engine.


/**
 * Block CPU for the given amount of seconds
 * @param {Number} [seconds]
 */
function slowdown(seconds = 0.5) {
  const start = (new Date()).getTime()
  while ((new Date()).getTime() - start < seconds * 1000){}
}

slowdown(2)
console.log('done')

Calling this method will slow code down for the given amount of seconds (with ~200ms precision).


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).


Generate an array of numbers in reverse order and sort it.

var slowDown = function(n){
  var arr = [];
  for(var i = n; i >= 0; i--){
    arr.push(i);
  }
  arr.sort(function(a,b){
    return a - b;
  });
  return arr;
}

This can be called like so:

slowDown(100000);

Or whatever number you want to use.