-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperformance-fps.min.js
More file actions
1 lines (1 loc) · 2.44 KB
/
performance-fps.min.js
File metadata and controls
1 lines (1 loc) · 2.44 KB
1
!function(e,s){"object"==typeof exports&&"undefined"!=typeof module?module.exports=s():"function"==typeof define&&define.amd?define(s):e.PerformanceFps=s()}(this,function(){"use strict";return class{static fpsToMs(e){return 1/e*1e3}static msToFps(e){return 1e3/e}constructor(e){this.events={},this.props=Object.assign({min:-2,max:2,start:0,samples:100,accuracy:64,delay:2e3,maxFps:60,minFps:30,checkFps:54,upperCheckFps:58,maxTryToUpper:1,reCheckAfter:!1},e),this.now=(()=>performance.now()||Date.now),this.reset(),this.pause=this.pause.bind(this),document.addEventListener("visibilitychange",this.pause,!1)}getEvent(e){return void 0===this.events[e]&&(this.events[e]=new Set),this.events[e]}on(e,s){this.getEvent(e).add(s)}emit(e,...s){this.getEvent(e).forEach(e=>{e.apply(this,s)})}removeListener(e,s){this.getEvent(e).delete(s)}pause(){this.isPaused=document.hidden,this.reset()}destroy(){document.removeEventListener("visibilitychange",this.pause,!1),this.removeListener("change",this.performance)}reset(e){const{start:s,maxFps:t,reCheckAfter:i,checkFps:r}=this.props;this.upper=0,this.failIncrement=0,this.ms=this.constructor.fpsToMs(t),this.average=this.ms,e||(this.prev=this.now(),this.performance=s,this.reCheckAfter=i,this.checkCurrentFps=r,this.isTooLow=!1,this.elapsedTime=0,this.resetTime=0,this.store=[],this.storedMs=0),this.delay||(this.delay=this.props.delay)}setPerformance(){const{min:e,max:s,minFps:t,maxTryToUpper:i,checkFps:r,upperCheckFps:h}=this.props;this.constructor.msToFps(this.average)<=t?(this.performance=e,this.isTooLow=!0):this.constructor.msToFps(this.average)<this.checkCurrentFps?(this.performance-=1,this.upper>0&&(this.failIncrement+=1),this.checkCurrentFps=r):this.constructor.msToFps(this.average)>h&&this.failIncrement<i&&this.performance<s&&(this.upper+=1,this.performance+=1,this.checkCurrentFps=h),this.prevPerformance!==this.performance&&this.emit("change",this.performance),this.prevPerformance=this.performance}update(){if(this.isTooLow||this.isPaused)return;const{accuracy:e,min:s,samples:t}=this.props,i=this.now(),r=i-this.prev;if(this.prev=i,this.ms=r,!(i<this.delay)&&(this.storedMs+=r,this.storedMs>=e&&this.performance>s)){if(0===r)return;this.prevPerformance=this.performance,this.delay=0,this.reCheckAfter&&i>this.reCheckAfter&&(this.reset(!0),this.reCheckAfter+=i,this.resetTime+=1),this.store.push(r),this.store.length>t-1&&(this.average=this.store.reduce((e,s)=>e+s)/this.store.length,this.setPerformance(),this.store=[]),this.storedMs=0}}}});