Home / Get the milliseconds with Javascript and measure time

Get the milliseconds with Javascript and measure time

If you need to measure how long something takes with Javascript you can use the Date() object to get a start timestamp and then compare it later on using a second Date() object. Although I’m sure there are other benchmarking plugins etc available this can be a quick and easy way to benchmark something in Javascript.

First get the start timestamp as follows. This is the current time in milliseconds.

var start = new Date().getTime();

Then do your processing and substract start from the current time in millseconds.

var elapsed = new Date().getTime() - start;

This will give you the time taken between the two points in time in milliseconds.