Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 716
François Beaufort Avatar asked Sep 19 '12 10:09

François Beaufort


People also ask

How do you use the console log function in JavaScript?

log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console. log(A);

How do you use console timeEnd and console time?

You first call console. time() by providing a string argument, then the code that you want to test, then call console. timeEnd() with the same string argument. You'll then see the time it took to run the code in your browser console.

What is console time () and console timeEnd ()? What is its syntax and why is it used?

Definition and UsageThe timeEnd() method ends a timer, and writes the result to the console. The timeEnd() method allows you to time code operations for testing purposes.


2 Answers

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

like image 100
François Beaufort Avatar answered Sep 21 '22 11:09

François Beaufort


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

like image 43
nikoss Avatar answered Sep 21 '22 11:09

nikoss