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" />
This post is available on GitHub.
Posted .

Comments

Leave a Reply