Skip to content

Instantly share code, notes, and snippets.

@gi11es
Created December 5, 2018 20:48
Show Gist options
  • Save gi11es/6b48c2442cfcd6d71444a598bd5d3031 to your computer and use it in GitHub Desktop.
Save gi11es/6b48c2442cfcd6d71444a598bd5d3031 to your computer and use it in GitHub Desktop.
CPU benchmark
function cpuBenchmark() {
var blob, worker, work;
function onMessage() {
var i,
startTime,
amount = 100000000;
// IE11 doesn't have window.performance exposed inside workers
if ( !self.performance ) {
postMessage( false );
return;
}
startTime = performance.now();
for ( i = amount; i > 0; i-- ) {
// empty
}
postMessage( Math.round( performance.now() - startTime ) );
}
work = 'onmessage = ' + String( onMessage );
blob = new Blob( [ work ], { type: 'application/javascript' } );
worker = new Worker( URL.createObjectURL( blob ) );
worker.onmessage = function ( e ) {
console.log( e.data );
worker.terminate();
};
worker.postMessage( false );
}
cpuBenchmark();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment