0

I can easily do this:

console.time('mytimer');
doSomeWork();
console.timeEnd('mytimer');

But is it possible to calculate time in multiple functions. I need to define the script's start time in a global variable. Then inside multiple functions I will write how many miliseconds passed since the start of the time. And write the name of the function Something like this:

console.time('mytimer');
doSomeWork() {
  // console.log(difference between now and "mytimer"s start time)
  // console.log(name of the function: doSomeWork())
};
doSomeWork2() {
  // console.log(difference between now and "mytimer"s start time)
  // console.log(name of the function: doSomeWork2())
};
doSomeWork3() {
  // console.log(difference between now and "mytimer"s start time)
  // console.log(name of the function: doSomeWork3())
};
console.timeEnd('mytimer');

I will use this in Chrome 26+ for debug issues so using browser dependent functions (for example: arguments.callee.name) is not a problem.

Edit: To clearize my problem.
This works:

console.time('myTimer1');
console.timeEnd('myTimer1');

This doesn't work:

console.time('myTimer2');
console.time('myTimer2');

Edit: Of course it is possible to write too much timers and check time of each of them. But I need to know elapsed time since the javascript code is started in each lap.

trante
  • 33,518
  • 47
  • 192
  • 272
  • You have already answered your question, using a global variable and arguments.callee.name. What exactly you need as an answer then? – Vishwanath May 26 '13 at 13:37
  • I couldn't get the time difference with console.time. – trante May 26 '13 at 14:23
  • ISTM the profiler (“Profiles" tab) is made for this. – PointedEars May 27 '13 at 07:10
  • console.time('myTimer2'); console.timeEnd('myTimer2'); Did u meant this? – Vishwanath May 27 '13 at 07:17
  • ```console.timeEnd``` stops the timer and I can't reach the "myTimer2" anymore. – trante May 27 '13 at 20:01
  • @PointedEars Profiles tab is good for general behaviour. I need to examine specific points with this, and it's hard to select javascript parts from that. – trante May 28 '13 at 11:12
  • What do you think of the answer posted? – Vishwanath May 28 '13 at 12:13
  • Regarding to [this question](http://stackoverflow.com/questions/313893/how-to-measure-time-taken-by-a-function-to-execute) it is not recommended to use Date() for performance timings. So after some time if I can't find an answer with console.time, I will accept it :) – trante May 28 '13 at 15:00

2 Answers2

1

If you need times on particular function, I guess you know that they can be achieved with argument to console.time() and console.timeEnd(). More info about it here https://developers.google.com/chrome-developer-tools/docs/console/ .

From my understanding of your question you need laps for benchmark analysis.

I have defined following methods which you can use to get lap times in milliseconds.


Update : Using performance api which is recommended API for such use cases.

console.lapStart = function(name){
     window[name] = window[name] || {};
     window[name].globalTimer = performance.now();
}
console.showLap = function(name){
     currTime = performance.now();
     var diff = currTime  - window[name].globalTimer;
     console.log(arguments.callee.name, diff);
}
console.lapEnd = function(name){
     currTime = performance.now();
     var diff = currTime  - window[name].globalTimer;
     console.log(arguments.callee.name, diff);
     delete window[name]
}

Note that this is rough code and should be run only in dev. Otherwise it might leave bad traces in global object and bad taste on your mouth.

Vishwanath
  • 6,284
  • 4
  • 38
  • 57
1

There is a solution which is a living standard. Seems like its already on chrome and major browsers and nodejs

https://developer.mozilla.org/en-US/docs/Web/API/Console/timeLog

console.time("answer time");
alert("Click to continue");
console.timeLog("answer time");
alert("Do a bunch of other stuff...");
console.timeEnd("answer time");
The
Faiz Mohamed Haneef
  • 3,418
  • 4
  • 31
  • 41