67

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

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

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

Is this something doable?

2
  • Before you need to put console.time('t'); Commented Sep 17, 2017 at 12:35
  • no need label, default label is default Commented Aug 30, 2019 at 15:39

4 Answers 4

64

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()

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

Sign up to request clarification or add additional context in comments.

2 Comments

Would be nice if console.timeEnd() just returned the elapsed time
I slapped this util class together based on performance.now() as it's widely accepted - caniuse.com/?search=performance.now - stackblitz.com/edit/benchy?file=benchy.ts,index.ts
29

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

1 Comment

See this article for reasons why window.performance.now() may be a better option: developers.google.com/web/updates/2012/08/… The API is well supported now, too: caniuse.com/#search=performance.now
8

Small helper to do some time measuring. timerEnd returns the time in ms, also the timers object 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.

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;
}

Comments

-6

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

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

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

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

2 Comments

Actually, seems it is working in FireBug, but not Chrome Inspector.
As Console API is not an official standard, I guess this explains why they behave differently.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.