Search code examples
javascriptframe-rate

function to calculate function fps


Ok so I believe I could best describe the issue through code so here goes

var clicks = 0;

//function to calculate
function clicking(){
  clicks += 1;
}

//function to calculate fps where fn is the name of the function
function FPS(fn){
  //do stuff
}

Okay so to clarify I dont want to add a variable to the actual function clicking I would like to be able to call something like FPS(clicking) and have the function return a value for example

var fps = FPS(clicking);

then i could display the returned number as such element.innerHTML = fps

EDIT: I know with the current code it seems silly but this is just example coding not what I am actually using


Solution

  • This is not very actual since Date.now() also uses time.

    function FPS(fn) {
      var startTime = Date.now();
      fn();
      var endTime = Date.now();
    
      return endTime - startTime;
    }
    
    function longClick() {
      var abc = 0;
      for (var i = 0; i < 100000000; i++) {
        abc++;
      }
    }
    
    var fps = FPS(longClick);
    console.log((fps / 1000) + ' seconds');


    FPS usually refers to Frames Per Second which is the frequency of refreshing screen image.

    Pick a more comprehensive name, with keywords like Elapsed, for teammates.