Question

I am currently logging an AJAX application with messages which include the times of certain interactions. So I have a couple of places where the code follows a pattern such as this:

var startTime = new Date();
this.doFunction();
var endTime = new Date();
logger.log("doFunction took " + (endTime - startTime) + " milliseconds.");

What I'm look to do is separate the timing into one library function, which takes a function as a parameter, to look something like:

time : function(toTime) {
    var startTime = new Date();
    eval(toTime);
    var endTime = new Date();
    logger.log(toTime + " took " + (endTime - startTime) + " milliseconds.");
} 

(Syntax may be wrong, I'm not too familiar with JavaScript)

So that then instead of doing the timing I would just do:

time(this.doFunction);

My question is, do different browsers have different behaviour when it comes to eval()? Such as firing off the eval into a new thread, thus rendering my timing incorrect?

Any other advice on timing would be appreciated.

Was it helpful?

Solution

No. All browsers are single-threaded in the javascript engine. I suspect that you can also solve this problem simply by invoking toTime() as a function instead of using eval(). You may want to look into the javascript arguments object and the javascript "call" and "apply" methods to transparently forward the arguments passed to your outer "time" function to the inner "toTime" function.

OTHER TIPS

Eval should be synchronous.

You shouldn't use eval() but toTime.call(), because you should avoid using eval.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top