Html5 game with a canvas, performance problems on firefox (4)

I am currently working on a small js / canvas game called "tunnel 2" (I'm sure there is a famous summer version of this there, but I don't know anything). You can try the game here . Also, I would recommend chrome.

So, I developed chrome on google, and it works fine even on my crappy old machine. I will bypass ~ 30 frames per second. on my colleague the laptop gives> 100 frames per second. So far, so good. safari seems to work well too.

Next, I tried this on firefox 4 beta 10 ... and I only get ~ 10 frames per second. but right ff4 is not so slow, right?

I started to research. here is my main loop:

// starts the game loop
this.run = function () {
  this.draw();

  var
    t = this,
    timeLastTurn,
    timeThisTurn = (new Date()).getTime()-1;

  var loop = function () {
    timeLastTurn = timeThisTurn;
    timeThisTurn = (new Date()).getTime();

    // dt is the time difference between this turn and the last turn
    var dt = timeThisTurn - timeLastTurn;

    // player movement etc
    t.turn(dt);

    // draw game state
    var res = t.draw();

    // if there no collision, game over
    if (!res.collision)
      t.setState(2);

    // actually, there a browser dependent minimum timeout that
    // may vary. but even if it more than 10ms - we don't care.
    // game should run at the same speed (though not as smooth)
    if (gameState == 1)
      timer = window.setTimeout(loop, 5);

    // just debug output
    debug = dt;
  }

  // start the main loop
  loop();
}

what i noticed:

, this.draw(); , ( 5, ), ... firefox. s > 100 10 ! loop() , firefox 10 !

, dt. time-loop() - + 5 - ( - ).

ff4 180 , 170 5 ! , ~ 800 (gc, ), 180 .

- , ?

? , , , , 150 !? , , . ? - gc ( 0,10%), Firebug .

: (~ 5 ) firebug.

. info: setInterval setTimeout .

+3
2

, FF 3.6.13 OS X.

Snake JS, setInterval. :

var timer = setInterval(fn, 500)
// ...
timer = setInterval(fn, 500)
// Since I didn't use clearInterval() I now have two timers running in FF.

, setInterval, , , setTimeout?

0

, firefox 6 . , firefox. Firefox 7, , , , . , ff4 facebook, 6 . , , , .

0

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


All Articles