Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
/**
* @param fn, the function to execute
* @param args, a single argument or an array
of arguments expected by param "fn"
* @param optionalDisplayName, an optional name to
show in the benchmark output
*/
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";
}
// 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()" );