Skip to content

Instantly share code, notes, and snippets.

@arjankuijpers
Created November 5, 2014 00:38
Show Gist options
  • Save arjankuijpers/be1589fe851cf9996b3b to your computer and use it in GitHub Desktop.
Save arjankuijpers/be1589fe851cf9996b3b to your computer and use it in GitHub Desktop.
fps / tick counter with settable average, ported it from somebody (on the web) that wrote it in a typesafe language..
//cant use const because of IE9, IE9 does not support const.
var MAXSAMPLES = 100;
var tickindex=0
var ticksum=0;
var ticklist = [];
for(var i = 0; i < MAXSAMPLES; i++)
{
ticklist[i] = 0;
}
var CalcAverageTick = function(newtick)
{
ticksum-=ticklist[tickindex]; /* subtract value falling off */
ticksum+=newtick; /* add new value */
ticklist[tickindex]=newtick; /* save new value so it can be subtracted later */
if(++tickindex==MAXSAMPLES) /* inc buffer index */
tickindex=0;
/* return average */
return(ticksum/MAXSAMPLES);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment