DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Function.prototype.bench

09.16.2005
| 4505 views |
  • submit to reddit
        AOP style benchmark.
Function#bench to attach the benchmark to an existing function.
Function.prototype.bench = function(st){
	var self = this;
	return function(){
		var start = new Date();
		var res = self.apply(this,arguments);
		var end = new Date();
		// to customize
		window.status = st + " : " + (end-start);
		return res;
	}
}
// example
var func = function(){
	var array = [];
	for(var i=0;i<100000;i++){
		array.push(i)
	}
}
// create [benchmark enabled] function
var func_bench = func.bench("create Array(100000)");
func_bench();
you can also
// works in transparent
func = func.bench()