Browser console and calculating multiple javascript execution time differences

感情迁移 提交于 2019-12-02 10:25:27

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.

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