Browser console and calculating multiple javascript execution time differences

人走茶凉 提交于 2019-12-02 18:35:43

问题


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.


回答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.




回答2:


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


来源:https://stackoverflow.com/questions/16759647/browser-console-and-calculating-multiple-javascript-execution-time-differences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!