Skip to content

Instantly share code, notes, and snippets.

@mattwiebe
Last active March 2, 2016 15:45
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 mattwiebe/059c8e5012e7438a8d39 to your computer and use it in GitHub Desktop.
Save mattwiebe/059c8e5012e7438a8d39 to your computer and use it in GitHub Desktop.
Check performance up to a certain point in the page's load, multiple times
/**
* To use: call the function at the point in the load you want to check performance for.
* The page will then reload for @var iterations times to provide multiple measures
*/
function checkPerf() {
var mean;
var iterations = 10;
var times = localStorage.getItem( 'perf' ) ? JSON.parse( localStorage.getItem( 'perf' ) ) : [];
times.push( performance.now() );
localStorage.setItem( 'perf', JSON.stringify( times ) );
if ( times.length >= iterations ) {
mean = times.reduce( (p,c) => p + c, 0 ) / times.length;
console.log( Math.round( mean ) + ' ms is the mean over ' + times.length + ' iterations', times );
alert( 'Performance checking done. Check the console for results.' );
localStorage.removeItem( 'perf' );
return;
} else {
location.reload();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment