Skip to content

Instantly share code, notes, and snippets.

@joshbeckman
Created January 22, 2018 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbeckman/383a08fec965a94ddd685c0345bcc605 to your computer and use it in GitHub Desktop.
Save joshbeckman/383a08fec965a94ddd685c0345bcc605 to your computer and use it in GitHub Desktop.
import perf from '../lib/perf.js';
const THRESHOLD = 83; // ~ 5 animation frames
const slowLog = (action, duration) => {
(requestAnimationFrame || setTimeout)(() => {
console.warn(`[perf] action ${action.type} took ${duration.toFixed(2)}ms`, {
mean: perf.mean(action.type),
sdev: perf.sdev(action.type),
samples: perf.getEntriesByName(action.type).length,
});
}, 0);
};
let correction = 0;
perf.onFPS((fps) => {
correction = 60 - fps;
}, 500);
const perfMiddleware = store => next => action => {
if (!action._delayed && correction > 5) {
action._delayed = correction;
setTimeout(() => {
store.dispatch(action);
}, Math.round(correction));
return store.getState();
}
perf.start(action.type);
let result = next(action);
perf.end(action.type);
if (perf.duration(action.type) > THRESHOLD) {
slowLog(action, perf.duration(action.type));
}
return result;
};
export default perfMiddleware;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment