Is it possible to slow down with Javascript Interpreter?

I want my browser to execute javascript code more slowly. Is there a browser addon or something like that?

Background . I want to demonstrate the impact of runtime on this browser game. The speed of this game is limited only by the speed of its code execution, as in the good old MS-DOS games. To show students the impact of such coding (depending on the processor cycles, and not on time), it would be great if I could reduce the speed of code execution.

Any ideas?

Thanks!

+4
source share
3 answers

JavaScript - , , .

function sleep(µs) {
     var end = performance.now() + µs/1000;
     while (end > performance.now()) ; // do nothing
}
+1

, ( , ) :

function fibo(max) {
    var x = 0;
    for (var i = 0, j = 1, k = 0; k < max; i = j, j = x, k++) {
        x = i + j;
    }
}

:

doSomething();
fibo(15);
doSomethingElse();
fibo(15);
doSomethingElseElse();
fibo(15);
0

/

function loop() {
  draw();
  loop();
}

~ 100

function loop() {
  draw();
  setTimeout(loop, 100);
}

-, , , ,

function draw(delta) {
  console.log("%f ms", delta);
}

function loop(last) {
  var now = performance.now();
  var delta = now - last;
  draw(delta);
  setTimeout(function() { loop(now); }, 100);
}

loop(0);

// 102.51500000001397 ms
// 102.38000000000466 ms
// 105.33499999996275 ms
// 101.27500000002328 ms
// 103 ms
// 100.88000000000466 ms
// 100.9649999999674 ms
// 100.69500000000698 ms
// 102.01500000001397 ms
// 105.85499999998137 ms

, 100 . 10 . , .

function framesPerSecond(x) {
  return 1e3/x;
}

function draw(delta) {
  console.log("%f ms", delta);
}

function loop(fps, last) {
  var now = performance.now();
  var delta = now - last;
  draw(delta);
  setTimeout(function() { loop(fps, now); }, fps);
}

loop(framesPerSecond(25), 0); // 25 frames per second
// 42.8150000000023 ms
// 42.1600000000035 ms
// 44.8150000000023 ms
// 41.8299999999872 ms
// 43.195000000007 ms
// 41.7250000000058 ms
// 42.1349999999802 ms
// 43.0950000000012 ms

loop(framesPerSecond(0.5), 0); // 1 frame per 2 seconds
// 2001.58 ms
// 2002.705 ms
// 2005.9 ms
// 2001.665 ms
// 2002.59 ms
// 2006.165 ms

. framesPerSecond , draw - . , , draw fps.

0

Source: https://habr.com/ru/post/1614185/


All Articles