841Timing with console.time()

Everyone working with on more complex JS application sooner or later has the need to time operations.

This code is purely for historic purposes and standing reminder to check out the Chrome DevTools Documentation before writing your own utility functions.

// Timer Function
(function() {
    self.timer = timer;
    timer.version = "1.0.0";
    timer.names = {};

    function timer(name) {
        return {
            start: function() {
                timer.names[name] = {};
                timer.names[name].start = new Date().getTime();
                console.log("Timer '" + name + "' started.");
            },
            stop: function() {
                if (timer.names[name] && timer.names[name].start) {
                    var diff = new Date().getTime() - timer.names[name].start;
                    console.log("Timer '" + name + "' took " + diff/1000 + " sec.");
                    delete timer.names[name];       // remove timer object
                }   

            },
            test: function() {
                console.log(timer.names);
            }
        }
    }

})();

// Usage

timer("test").start();
timer("test").stop();   // logs elapses time..

The perfect built-in solution is console.time() and console.timeEnd():

console.time("test");
console.timeEnd("test");    // logs elapses time..

Still, I have to say, I was quite pleased how my own function turned out..