Skip to content

Instantly share code, notes, and snippets.

@danihodovic
Last active August 29, 2015 14:09
Show Gist options
  • Save danihodovic/f8bdd2d18b0d44d330a1 to your computer and use it in GitHub Desktop.
Save danihodovic/f8bdd2d18b0d44d330a1 to your computer and use it in GitHub Desktop.
Benchmark
function benchmark(args) {
var error_msg = [
"Provide an object with at least a func keyword, example:",
"{func: function, thisarg: thisarg, times:times}",
"func must be an instanceof Function"
].join('\n');
return function() {
if (!(args.func instanceof Function)) {
throw new Error(error_msg)
}
var func = args.func
var thisarg = args.thisarg
var times = args.times || 1
var total_time = 0
var to_return
for (var i = 0; i < times; i++) {
var start_time = new Date().getTime()
to_return = func.apply(thisarg, arguments)
total_time += new Date().getTime() - start_time
}
console.info('Function', func.name, 'total', total_time, 'ms')
console.info('Function', func.name, 'avg', total_time / times, 'ms')
return to_return
}
}
//Usage
benchmark({
func: func,
times: 1000,
thisarg: {
hello: 'world'
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment