Simple Javascript Timer

Simple Javascript Timer

Sometime we may need to create a timer to count how long the javascript is perform from begin and end. Below is a simple code that give a little help.

var timer = {

    start: function() {
       this.begin = new Date().valueOf();
    },
    stop : function() {
       return new Date().valueOf() - this.begin;
    }

}

Usage

var timer = {

    start: function() {
       this.begin = new Date().valueOf();
    },
    stop : function() {
       return new Date().valueOf() - this.begin;
    }
}

timer.start();

var x = 0;
for ( i = 0; i < 100000; i ++) {
    x = i+i * (2);
}
console.log("x = " + x);

timer.stop();

 

Write a Reply or Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.