I’m doing some loading of data into a grid and I wanted to have a simple way of timing various method calls in JavaScript, so here is what I came up with.
function timeIt(func) {
var start = (new Date()).getTime();
var ret = func();
var end = (new Date()).getTime();
alert(end - start);
return ret;
}
You can take code like this.
var json = parseJson(data);
dataBindGrid('TheGrid', json);
And turn it into this.
var json = timeIt(function() { return parseJson(data); });
timeIt(function() { dataBindGrid('TheGrid', json); });
Notice that if you need a return value from the function you are timing you must place a return in the anonymous function that is passed in so that your variable is assigned. In this case the variable being assigned is “json”.