Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dgramlich committed Jul 10, 2013
0 parents commit a8d80e8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
console.time polyfill
=====================

Lightweight and simple polyfill for `console.time()` and `console.timeEnd()`

## Example usage:

```
# Start the timer by passing it a key
console.time('myTimer');
# Call timeEnd later to see how long an operation took
setTimeout(function() {
console.timeEnd('myTimer');
}, 2000);
# It should then log out something like like..
# myTimer: 2000ms
```

For more information on `console.time()` visit [MDN] (https://developer.mozilla.org/en-US/docs/Web/API/console.time).
49 changes: 49 additions & 0 deletions console-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
;(function( console ) {

'use strict';

var timers;

// do we have access to the console
// or does time method already exist?
if ( !console || console.time ) {
return;
}


// table of current timers
timers = {};


/**
* Stores current time in milliseconds
* in the timers map
*
* @param {string} timer name
* @return {void}
*/
console.time = function( name ) {
if ( name ) {
timers[ name ] = Date.now();
}
};


/**
* Finds difference between when this method
* was called and when the respective time method
* was called, then logs out the difference
* and deletes the original record
*
* @param {string} timer name
* @return {void}
*/
console.timeEnd = function( name ) {
if ( timers[ name ] ) {
console.log( name + ': ' + (Date.now() - timers[ name ]) + 'ms' );
delete timers[ name ];
}
};


}( window.console ));

0 comments on commit a8d80e8

Please sign in to comment.