46

How could I calculate the FPS of a canvas game application? I've seen some examples, but none of them use requestAnimationFrame, and im not sure how to apply their solutions there. This is my code:

(function(window, document, undefined){

    var canvas       = document.getElementById("mycanvas"),
        context      = canvas.getContext("2d"),
        width        = canvas.width,
        height       = canvas.height,
        fps          = 0,
        game_running = true,
        show_fps     = true;

    function showFPS(){
        context.fillStyle = "Black";
        context.font      = "normal 16pt Arial";

        context.fillText(fps + " fps", 10, 26);
    }
    function gameLoop(){

        //Clear screen
        context.clearRect(0, 0, width, height);

        if (show_fps) showFPS();

        if (game_running) requestAnimationFrame(gameLoop);

    }
    
    gameLoop();

}(this, this.document))
canvas{
    border: 3px solid #fd3300;
}
<canvas id="mycanvas" width="300" height="150"></canvas>

By the way, is there any library I could add to surpervise performance?

Braiam
  • 1
  • 11
  • 47
  • 78
Enrique Moreno Tent
  • 24,127
  • 34
  • 104
  • 189

12 Answers12

44

Do not use new Date()

This API has several flaws and is only useful for getting the current date + time. Not for measuring timespans.

The Date-API uses the operating system's internal clock, which is constantly updated and synchronized with NTP time servers. This means, that the speed / frequency of this clock is sometimes faster and sometimes slower than the actual time - and therefore not useable for measuring durations and framerates.

If someone changes the system time (either manually or due to DST), you could at least see the problem if a single frame suddenly needed an hour. Or a negative time. But if the system clock ticks 20% faster to synchronize with world-time, it is practically impossible to detect.

Also, the Date-API is very imprecise - often much less than 1ms. This makes it especially useless for framerate measurements, where one 60Hz frame needs ~17ms.

Instead, use performance.now()

The Performance API has been specificly made for such use cases and can be used equivalently to new Date(). Just take one of the other answers and replace new Date() with performance.now(), and you are ready to go.

Sources:

Also unlike Date.now(), the values returned by Performance.now() always increase at a constant rate, independent of the system clock (which might be adjusted manually or skewed by software like NTP). Otherwise, performance.timing.navigationStart + performance.now() will be approximately equal to Date.now().

https://developer.mozilla.org/en-US/docs/Web/API/Performance/now

And for windows:

[The time service] adjusts the local clock rate to allow it to converge toward the correct time. If the time difference between the local clock and the [accurate time sample] is too large to correct by adjusting the local clock rate, the time service sets the local clock to the correct time.

https://technet.microsoft.com/en-us/library/cc773013(v=ws.10).aspx

maja
  • 17,250
  • 17
  • 82
  • 125
38

Chrome has a built-in fps counter: https://developer.chrome.com/devtools/docs/rendering-settings

enter image description here

Just open the dev-console (F12), open the drawer (Esc), and add the "Rendering" tab.

Here, you can activate the FPS-Meter overlay to see the current framerate (incl. a nice graph), as well as GPU memory consumption.

Cross-browser solution: You can get a similar overlay with the JavaScript library stat.js: https://github.com/mrdoob/stats.js/

enter image description here

It also provides a nice overlay for the framerate (incl. graph) and is very easy to use.

When comparing the results from stats.js and the chrome dev tools, both show the exact same measurements. So you can trust that library to actually do the correct thing.

maja
  • 17,250
  • 17
  • 82
  • 125
27

You could keep track of the last time requestAnimFrame was called.

var lastCalledTime;
var fps;

function requestAnimFrame() {

  if(!lastCalledTime) {
     lastCalledTime = Date.now();
     fps = 0;
     return;
  }
  delta = (Date.now() - lastCalledTime)/1000;
  lastCalledTime = Date.now();
  fps = 1/delta;
} 

http://jsfiddle.net/vZP3u/

Andy Ray
  • 30,372
  • 14
  • 101
  • 138
Justin Thomas
  • 5,680
  • 3
  • 38
  • 63
  • 1
    One; getTime returns the time in milisecond, so you should use 1000/delta. Second; you never update lastCalledTime. Third; requestAnimationFrame send a date object as the first parameter with the time of the callback, so no need to create new Date objects. – Gerben Nov 26 '11 at 17:28
  • 2
    The answer use to include both `Date.now()` and `new Date().getTime()` which was confusing. I edited the answer to use `Date.now()` in both places, as they return [the same thing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now#Polyfill) – Andy Ray Jan 25 '16 at 00:45
  • 2
    I highly recommend just using https://github.com/mrdoob/stats.js/ (it even has a bookmarklet version) – reflog Feb 05 '17 at 16:18
  • 3
    Do NOT use `Date()`. Its very imprecise (often less than 1ms) and can even return wrong data if the operating system is currently synchronizing its clock with an NTP timer server (which happens all the time). Also don't forget time zones and all that stuff. Instead, use the Performance API: https://developer.mozilla.org/en-US/docs/Web/API/Performance/now - it was developed for exactly this use case. – maja Jul 17 '17 at 11:22
  • agreed with @maja, don't use the Date, but don't even use performance.now. requestAnimationFrame passes an highResTimestamp to its callback. – Kaiido Mar 04 '18 at 03:24
  • @Kaiido you're right - when you use `requestAnimationFrame`, you don't need to use the performance API directly - the browser already provides a suitable timestamp. But you'll need the API for all other cases where you want to measure timings in JavaScript. – maja Mar 04 '18 at 14:48
  • Here's a refactored version (es6 with better readability) of the same fiddle: http://jsfiddle.net/go5dvfzc/78/ – Jalal Mar 19 '22 at 14:27
11

Here's another solution:

var times = [];
var fps;

function refreshLoop() {
  window.requestAnimationFrame(function() {
    const now = performance.now();
    while (times.length > 0 && times[0] <= now - 1000) {
      times.shift();
    }
    times.push(now);
    fps = times.length;
    refreshLoop();
  });
}

refreshLoop();

This improves on some of the others in the following ways:

  • performance.now() is used over Date.now() for increased precision (as covered in this answer)
  • FPS is measured over the last second so the number won't jump around so erratically, particularly for applications that have single long frames.

I wrote about this solution in more detail on my website.

Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
8

I have a different approach, because if you calculate the the FPS you'll get this flickering when returning the number. I decided to count every Frame and return it once a second

window.countFPS = (function () {
  var lastLoop = (new Date()).getMilliseconds();
  var count = 1;
  var fps = 0;

  return function () {
    var currentLoop = (new Date()).getMilliseconds();
    if (lastLoop > currentLoop) {
      fps = count;
      count = 1;
    } else {
      count += 1;
    }
    lastLoop = currentLoop;
    return fps;
  };
}());

requestAnimationFrame(function () {
  console.log(countFPS());
});

jsfiddle

Ismail
  • 8,904
  • 3
  • 21
  • 39
kernel
  • 3,654
  • 3
  • 25
  • 33
  • 8
    Was it so hard to copy-paste the code into jsfiddle yourself? Here's what you wanted http://jsfiddle.net/krnlde/u1fL1cs5/ – kernel Jan 12 '15 at 15:35
8

I was missing an implementation that allows to customize the size of the sample for the averaged FPS value. Here is mine , it has the following features :

  • Accurate : performance.now() based
  • Stabilized : Returned FPS value is an averaged value ( fps.value | fps.tick() )
  • Configurable : FPS samples array size can be customized ( fps.samplesSize )
  • Efficient : Rotatory array for collecting samples (avoids array resizing)

const fps = {
    sampleSize : 60,    
    value : 0,
    _sample_ : [],
    _index_ : 0,
    _lastTick_: false,
    tick : function(){
        // if is first tick, just set tick timestamp and return
        if( !this._lastTick_ ){
            this._lastTick_ = performance.now();
            return 0;
        }
        // calculate necessary values to obtain current tick FPS
        let now = performance.now();
        let delta = (now - this._lastTick_)/1000;
        let fps = 1/delta;
        // add to fps samples, current tick fps value 
        this._sample_[ this._index_ ] = Math.round(fps);
        
        // iterate samples to obtain the average
        let average = 0;
        for(i=0; i<this._sample_.length; i++) average += this._sample_[ i ];

        average = Math.round( average / this._sample_.length);

        // set new FPS
        this.value = average;
        // store current timestamp
        this._lastTick_ = now;
        // increase sample index counter, and reset it
        // to 0 if exceded maximum sampleSize limit
        this._index_++;
        if( this._index_ === this.sampleSize) this._index_ = 0;
        return this.value;
    }
}


// *******************
// test time...
// *******************

function loop(){
    let fpsValue = fps.tick();
    window.fps.innerHTML = fpsValue;
    requestAnimationFrame( loop );
}
// set FPS calulation based in the last 120 loop cicles 
fps.sampleSize = 120;
// start loop
loop()
<div id="fps">--</div>
colxi
  • 7,640
  • 2
  • 45
  • 43
7

Actually none of the answers were sufficient for me. Here is a better solution which:

  • Use's performance.now()
  • Calculates the actual average fps per second
  • Average per second and decimal places are configurable

Code:

// Options
const outputEl         = document.getElementById('fps-output');
const decimalPlaces    = 2;
const updateEachSecond = 1;

// Cache values
const decimalPlacesRatio = Math.pow(10, decimalPlaces);
let timeMeasurements     = [];

// Final output
let fps = 0;

const tick = function() {
  timeMeasurements.push(performance.now());

  const msPassed = timeMeasurements[timeMeasurements.length - 1] - timeMeasurements[0];

  if (msPassed >= updateEachSecond * 1000) {
    fps = Math.round(timeMeasurements.length / msPassed * 1000 * decimalPlacesRatio) / decimalPlacesRatio;
    timeMeasurements = [];
  }

  outputEl.innerText = fps;

  requestAnimationFrame(() => {
    tick();
  });
}

tick();

JSFiddle

Mick
  • 8,203
  • 10
  • 44
  • 66
4

Just check the difference in time between the AFR-callbacks. AFR already passes the time as an argument to the callback. I updated your fiddle to show it: http://jsfiddle.net/WCKhH/1/

Gerben
  • 16,747
  • 6
  • 37
  • 56
3

Just a proof of concept. Very simple code. All we do is set our frames per second and intervals between each frame. In the drawing function we deduct our last frame’s execution time from the current time to check whether the time elapsed since the last frame is more than our interval (which is based on the fps) or not. If the condition evaluates to true, we set the time for our current frame which is going to be the “last frame execution time” in the next drawing call.

var GameLoop = function(fn, fps){
    var now;
    var delta;
    var interval;
    var then = new Date().getTime();

    var frames;
    var oldtime = 0;

    return (function loop(time){
        requestAnimationFrame(loop);

        interval = 1000 / (this.fps || fps || 60);
        now = new Date().getTime();
        delta = now - then;

        if (delta > interval) {
            // update time stuffs
            then = now - (delta % interval);

            // calculate the frames per second
            frames = 1000 / (time - oldtime)
            oldtime = time;

            // call the fn
            // and pass current fps to it
            fn(frames);
        }
    }(0));
};

Usage:

var set;
document.onclick = function(){
    set = true;
};

GameLoop(function(fps){
    if(set) this.fps = 30;
    console.log(fps);
}, 5);

http://jsfiddle.net/ARTsinn/rPAeN/

yckart
  • 32,460
  • 9
  • 122
  • 129
2

My fps calculation uses requestAnimationFrame() and the matching timestamp argument for its callback function.
See https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame and https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp.

No need for new Date() or performance.now()!

The rest is inspired heavily by other answers in this thread, especially https://stackoverflow.com/a/48036361/4706651.

var fps = 1;
var times = [];
var fpsLoop = function (timestamp) {
    while (times.length > 0 && times[0] <= timestamp - 1000) {
        times.shift();
    }
    times.push(timestamp);
    fps = times.length;
    console.log(fps);
    requestAnimationFrame(fpsLoop);
}

requestAnimationFrame(fpsLoop);
sebix
  • 39
  • 7
2

The best way that I use with performance.now()
Simple I passed TIME on gameLoop function and calculate fps

fps = 1 / ( (performance.now() - LAST_FRAME_TIME) / 1000 );

(function(window, document, undefined){

    var canvas       = document.getElementById("mycanvas"),
        context      = canvas.getContext("2d"),
        width        = canvas.width,
        height       = canvas.height,
        fps          = 0,
        game_running = true,
        show_fps     = true,
        LAST_FRAME_TIME = 0;

    function showFPS(){
        context.fillStyle = "Black";
        context.font      = "normal 16pt Arial";

        context.fillText(fps + " fps", 10, 26);
    }
    function gameLoop(TIME){

        //Clear screen
        context.clearRect(0, 0, width, height);

        if (show_fps) showFPS();

        fps = 1 / ((performance.now() - LAST_FRAME_TIME) / 1000);

        LAST_FRAME_TIME = TIME /* remember the time of the rendered frame */

        if (game_running) requestAnimationFrame(gameLoop);

    }
    
    gameLoop();

}(this, this.document))
canvas{
    border: 3px solid #fd3300;
}
<canvas id="mycanvas" width="300" height="150"></canvas>
Ali Esmailpor
  • 1,209
  • 3
  • 11
  • 22
0

i had to create a function which sets on which fps should animation run, because i have a 240hz monitor and animations on my screen are much faster then on other screens, so that my end projects was always slower on other monitors

function setFPSandRunAnimation(fps, cb) {
    let frameCount = 0;
    let fpsInterval, startTime, now, then, elapsed;

    runAnimating(fps);

    function runAnimating(fps) {
        fpsInterval = 1000 / fps;
        then = Date.now();
        startTime = then;
        animate();
    }

    function animate(timestamp) {
        requestAnimationFrame(animate);
        now = Date.now();
        elapsed = now - then;
        if (elapsed > fpsInterval) {
            then = now - (elapsed % fpsInterval);
            const sinceStart = now - startTime;
            const currentFps = Math.round(1000 / (sinceStart / ++frameCount) * 100) / 100;
            const elapsedTime = Math.round(sinceStart / 1000 * 100) / 100;
            cb(timestamp, currentFps, elapsedTime)
        }
    }
}

this is how to you use it setFPSandRunAnimation(fpsSpeedYouWant, cbFunctionWhereYouGet timestamp, currentfps and elapsedTime).

inside of the cb function you can run any code you would run in animation function