-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dgramlich
committed
Jul 10, 2013
0 parents
commit a8d80e8
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 )); |