Javascript 如何从 JS 控制台中的 console.timeEnd() 获取输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12492979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:31:46  来源:igfitidea点击:

How to get the output from console.timeEnd() in JS Console?

javascriptgoogle-chromegoogle-chrome-devtools

提问by Fran?ois Beaufort

I'd like to be able to get the string returned from console.timeEnd('t')in my Google Chrome Javascript Console.

我希望能够从console.timeEnd('t')我的 Google Chrome Javascript 控制台中获取返回的字符串。

In this example below, I'd like one variable which would contain "t: 0.276ms"

在下面的这个例子中,我想要一个包含 "t: 0.276ms"

> console.time('t'); console.timeEnd('t');
  t: 0.276ms
< undefined

Is this something doable?

这是可行的吗?

采纳答案by Fran?ois Beaufort

In Google Chrome 23.0.1262.2 (Official Build 155904) dev, it looks like it's impossible. The only way I found to be able to calculate time with accuracy is to use window.performance.webkitNow()

在 Google Chrome 23.0.1262.2 (Official Build 155904) dev 中,这看起来是不可能的。我发现能够准确计算时间的唯一方法是使用window.performance.webkitNow()

Here's a simple example:

这是一个简单的例子:

var start = window.performance.now();
...
var end = window.performance.now();
var time = end - start;

Read more at http://updates.html5rocks.com/2012/08/When-milliseconds-are-not-enough-performance-now

http://updates.html5rocks.com/2012/08/When-milliseconds-are-not-enough-performance-now阅读更多信息

回答by nikoss

simply you can use

简单地你可以使用

var begin=Date.now();
something here ...;
var end= Date.now();

var timeSpent=(end-begin)/1000+"secs";

this is the simplest way and it will work on any browser not in only chrome

这是最简单的方法,它可以在任何浏览器上运行,而不仅仅是在 chrome 中

回答by Felix

Small helper to do some time measuring. timerEndreturns the time in ms, also the timersobject contains information about how many times the timer with this name was used, the sum of all measured times and the average of the measurements. I find this quite useful, since the measured time for an operation depends on many factors, so it's best to measure it several times and look at the average.

小帮手做一些时间测量。timerEnd以毫秒为单位返回时间,该timers对象还包含有关使用此名称的计时器的次数、所有测量时间的总和和测量平均值的信息。我发现这非常有用,因为测量的操作时间取决于许多因素,因此最好对其进行多次测量并查看平均值。

var timers = {};
function timer(name) {
    timers[name + '_start'] = window.performance.now();
}

function timerEnd(name) {
    if (!timers[name + '_start']) return undefined;
    var time = window.performance.now() - timers[name + '_start'];
    var amount = timers[name + '_amount'] = timers[name + '_amount'] ? timers[name + '_amount'] + 1 : 1;
    var sum = timers[name + '_sum'] = timers[name + '_sum'] ? timers[name + '_sum'] + time : time;
    timers[name + '_avg'] = sum / amount;
    delete timers[name + '_start'];
    return time;
}

回答by Josef Zajac

console.timeEnd() function puts the time to console, and returns the value so you can save it to variable

console.timeEnd() 函数将时间放入控制台,并返回值以便您可以将其保存到变量中

var c = console.timeEnd('a');
c/1000+'s';

or you can save this variable to window object for latest usage

或者您可以将此变量保存到 window 对象以供最新使用

window.c = console.timeEnd('b');
window.c