JSFiddle - React, Tailwind, and code Playground

by slace

JavaScript

var stopwatch = (function() {
    var start = 0;
    var end = 0;
    return {
        start: function() {
           var date = new Date();
           start = date.getTime();
        },
        stop: function() {
           var date = new Date();
           end = date.getTime();
           return end - start;
        },
        milliseconds: function() {
          return end - start;  
        },
        seconds: function() {
          return (end - start) / 1000;  
        },
        reset: function() {
         start = end = 0;  
        } 
    };
})();

function test() {
 return 1 + 1;   
}

stopwatch.start();
for(var i=0; i < 1e6; i++) {
  test.call();  
}
stopwatch.stop();
console.log('ms: ' + stopwatch.milliseconds());
console.log('s: ' + stopwatch.seconds());

stopwatch.reset();

stopwatch.start();
for(var i=0; i < 1e6; i++) {
  test.apply();  
}
stopwatch.stop();
console.log('ms: ' + stopwatch.milliseconds());
console.log('s: ' + stopwatch.seconds());