4

How can i time how much time passes between 2 events with javascript? like, to the millisecond?

Joel
19.4k3 gold badges67 silver badges84 bronze badges
asked Sep 27, 2009 at 2:49
1
  • 3
    It's worth noting that the accuracy of Javascript timing can be pretty questionable, both in terms of async code (setTimeout, setInterval) and in terms of polling duration (as with the answers given here). Commented Sep 27, 2009 at 6:52

6 Answers 6

16

When performing arithmetic operations on Date objects, they are implicitly converted to milliseconds (since 1970年01月01日 00:00:00 UTC), so all you need to do is subtract a Date created when the operation started from a Date created when the operation ends.

var start = new Date();
doSomeHeavyWork();
var end = new Date();
var millisecondsElapsed = end - start;
answered Mar 1, 2010 at 9:09
Sign up to request clarification or add additional context in comments.

Comments

15

Easiest way to do this.

console.time("timer name")
console.timeEnd("timer name")

This will output the time in milliseconds to the console.

answered Apr 4, 2014 at 22:59

Comments

5

It's surprisingly difficult to do.

var start = new Date();
// Do things here
var finish = new Date();
var difference = new Date();
difference.setTime(finish.getTime() - start.getTime());
alert( difference.getMilliseconds() );
answered Sep 27, 2009 at 2:57

2 Comments

If you just want the milliseconds, of course, you don't need to create a new date—you just subtract the two times. Also, you can shorten lines 4-5 into: var difference = new Date(finish.getTime() - start.getTime());
Oh for the love of ramen. If you have more than 999 milliseconds, this method will not work at all.
2

Well, you can use firebug (firefox plugin) to benchmark your functions. Check this article : benchmark javascript funcions

answered Sep 27, 2009 at 2:52

1 Comment

You can similarly use the Web Inspector in Safari and Chrome (and other WebKit browsers). The results in these tools will be more reliable and provide more information than manual Date polling.
2

What about making a reusable timer object?

Usage:

// event 1
document.getElementById('elId').onclick = function () {
 timer.start('myTimer1');
};
// event 2
document.getElementById('otherElement').onclick = function () {
 alert(timer.stop('myTimer1')); // alerts the time difference in ms
};

Implementation:

var timer = (function () {
 var startTimes = {}; // multiple start times will be stored here
 return {
 start: function (id) {
 id = id || 'default'; // set id = 'default' if no valid argument passed
 startTimes[id] = +new Date; // store the current time using the timer id
 },
 stop: function (id) {
 id = id || 'default';
 var diff = (+new Date - startTimes[id]); // get the difference
 delete startTimes[id]; // remove the stored start time
 return diff || undefined; // return the difference in milliseconds
 }
 };
}());
answered Sep 27, 2009 at 6:25

1 Comment

Awesome, this piece of code works perfectly for the requirement.
1
var initialTime = (new Date).getTime(), i = 55000;
(function() {
 while ( i-- ) {
 setTimeout( function(){}, 20 );
 }
})()
var finalTime = ( new Date ).getTime(), diff = (new Date);
diff.setTime( finalTime - initialTime );
alert( diff.getMilliseconds() + 'ms' )
answered Sep 27, 2009 at 2:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.