Suppose you want to do some ad hoc profiling (and you don't have a profiling tool handy). Here would be one way to do it in Java:
long t1 = System.currentTimeMillis();
slowMethod();
long t2 = System.currentTimeMillis();
long delta = t2 - t1;
System.out.println("slowMethod: " + delta);
And here would be the same thing in JavaScript:
executeAndTime(slowMethod);
This works because JavaScript treats functions as just-another-object. As such, the function can be passed around like any other object. In this example, slowMethod (an object of type function) is passed as an argument to the executeAndTime() function. Here is what
executeAndTime
looks like:
function executeAndTime(f){
var t1 = new Date();
f();
var t2 = new Date();
var delta = t2 - t1;
document.write("Delta for [" + f.name + "]: " + delta + " millis");
}