Last active
December 17, 2015 04:59
-
-
Save simondell/5554381 to your computer and use it in GitHub Desktop.
Profile JS operations easily! This function tells you the median number of times an operation will run per second, based on five separate counts. Pass it a name for the test and an operation to perform. Based on an idea in Raphaele Cecco's "Supercharged HTML Graphics", which in turn was inspired by code by John Resig.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @param: name - String, a label for the test - usually the function's name | |
* @param: operation - Function, the code to test | |
*/ | |
var profile = function ( name, operation ) { | |
var iterate = function ( operation ) { | |
var start = new Date().getTime(); | |
var elapsed = 0; | |
for( var itrs = 0; elapsed < 1000; itrs++ ) { | |
operation(); | |
elapsed = new Date().getTime() - start; | |
} | |
return itrs; | |
} | |
var results = []; | |
// run five sets of tests | |
for( var i = 5; i >= 0; i-- ){ | |
results.push( iterate( operation ) ); | |
} | |
results.sort(); | |
console.warn( name +' ran a median of '+ results[2] +' times.' ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// declaratively decide which of three paths to run in response to a passed value | |
var declarative = function ( value ) { | |
switch( value ) { | |
case 0: case 1: | |
doTheRightThing( value ); | |
break; | |
case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: | |
doTheOtherThing( value ); | |
break; | |
default: | |
doTheDefaultThing( value ); | |
} | |
} | |
// logically decide which of three paths to run in response to a passed value | |
var logical = function ( value ) { | |
if( value <= 1 ) { | |
doTheRightThing( value ); | |
} else if ( value >= 2 && value <= 9 ) { | |
doTheOtherThing( value ); | |
} else { | |
doTheDefaultThing( value ); | |
} | |
} | |
// the three different responses | |
var doTheRightThing = function ( data ) { console.log('Right:', data ); } | |
var doTheOtherThing = function ( data ) { console.log('Other:', data ); } | |
var doTheDefaultThing = function ( data ) { console.log('Default:', data ); } | |
// repeat one of the above function over a data set (the numbers 19 -> 0) | |
var repeat20 = function ( fn ) { | |
for( var count = 19; count >= 0; count-- ){ | |
fn( count ); | |
} | |
} | |
// call profile from the other gist item, with a name, and a function to run the iterator | |
profile( 'declarative', function () { repeat20( declarative )} ); | |
profile( 'logical', function () { repeat20( logical )} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment