Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time things with Javascript

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

like image 249
John Stimac Avatar asked Nov 27 '22 00:11

John Stimac


2 Answers

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;
like image 141
gustafc Avatar answered Dec 09 '22 18:12

gustafc


Easiest way to do this.

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

This will output the time in milliseconds to the console.

like image 34
James Teague II Avatar answered Dec 09 '22 18:12

James Teague II