Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

benchmarking tool

Status
Not open for further replies.

jemminger

Programmer
Jun 25, 2001
3,453
US
sick of reinventing the wheel every time i wanted to benchmark some functions, so i wrote a simple tool:
Code:
function benchmark(fn, args, optionalDisplayName) {
	var output = document.getElementById("benchmark-output");

	//  set up the output textarea
	if (!output) {
		var container = document.createElement("div");
		container.setAttribute("id", "benchmark-container");

		var title = document.createElement("div");
		title.setAttribute("id", "benchmark-title");
		title.appendChild( document.createTextNode("benchmark results") );

		output = document.createElement("textarea");
		output.setAttribute("id", "benchmark-output");
		output.style.width = "100%";
		output.style.height = "15em";

		container.appendChild(title);
		container.appendChild(output);

		document.body.appendChild(container);
	}

	// make sure args is an Array
	if (args && args.constructor != Array) args = [args];
	
	var iStart = new Date().getTime();
	fn.apply(this, args);
	var iStop = new Date().getTime();

	var name = optionalDisplayName || /(.+)\n/.exec(fn.toString())[1];
	output.value += name + " : " + ((iStop-iStart)/1000) + " sec\n";
}

Code:
usage:
//  no args
function foo() {
  // do something
}

benchmark( foo );

//  with args
function bar1(arg) {
  // do something
}
function bar2(arg1, arg2, arg3) {
  // do something
}

benchmark( bar1, "something" );
benchmark( bar2, ["something", "2", "3"] );

//  using optional display name since functions defined
//  like below don't report a name
document.foobar = function(arg) {
  // do something
}

benchmark( document.foobar, "blah", "foobar()" );

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
created faq216-6103


-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Jeff,

what's going on here:

Code:
fn.apply(this, args);

...and here:

Code:
/(.+)\n/.exec(fn.toString())[1]

?

Looks interesting! I've tried it in IE6 and Opera and it seems to work as expected.

Nice work!

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
apply() is a method of Function objects that lets you apply an array of arguments. this way you can dynamically call a function and pass a variable number of arguments to it.

the line
/(.+)\n/.exec(fn.toString())[1]

is a regex to extract the first line up to a line break from the function definition; the [1] is to use only the first parenthesized capture

this allows us to extract "function foo()" for display in the output from a function like
Code:
function foo() {
  // do stuff
}

anonymous functions defined like
Code:
document.foobar = function() {
  // whatever
}
don't report their names (they're anonymous, duh ;-) )... hence the optionalDisplayName parameter

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top