views:

4366

answers:

4

I need to get time in milliseconds. Please advise.

+6  A: 

Use Firebug, enable both Console and Javascript. Click Profile. Reload. Click Profile again. View the report.

Stefan Mai
Good advice but obviously works only for FF. We often want to compare browser speeds... :-)
PhiLho
+13  A: 

use Date().getTime()

The getTime() method returns the number of milliseconds since midnight of January 1, 1970.

ex.

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

for (i = 0; i < 50000; ++i) {
// do something
}

var end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time);

alternatively, getMilliseconds() will give the milliseconds of the current Date object.

Owen
works for me. Cheers.
J Angwenyi
Note that you can substitute +new Date() for the getTime() call:var start = +new Date();// do stuffalert("Execution time: "+(+new Date())-start);
J c
+7  A: 

Be aware that the results from new Date().getTime() may not give you milliseconds precision.

Take a look at this

Serhii
+4  A: 

You can use console.time: (only with firebug+firefox)

console.time('someFunction timer');

function someFunction(){ ... }

console.timeEnd('someFunction timer')
vsync