JavaScript Timer; JavaScript Duration; Measure the duration of a JavaScript event

To time and measure the duration of a JavaScript event.
function start() {
    startTime = new Date().getTime();
}
 
function stop() {
    var endTime = new Date().getTime();
    return endTime - startTime;
}
Example:
function start() {
    startTime = new Date().getTime();
}
 
function stop() {
    var endTime = new Date().getTime();
    return endTime - startTime;
}
 
function doSomething() {
    for (var i = 0; i < 1000000; i++) {
        // Do something
    }
}
<input value="Click me" onclick="start(); doSomething(); alert(stop() + 'ms');" type="button" />
Did this help you? Please let me know with a comment. Thank you
Posted .

Comments (RSS)

Leave a comment