Created
April 6, 2016 13:47
-
-
Save beckettkev/0e942194dbc7c0f3b456784ae5477973 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| The console.time functionality can be used for measuring performance of JavaScript functions (outputing processing times to the developer console window) | |
| https://developer.mozilla.org/en-US/docs/Web/API/Console/time | |
| */ | |
| // console.time implementation for IE | |
| if ( window.console && typeof ( window.console.time ) == "undefined") { | |
| console.time = function (name, reset) { | |
| if (!name) { | |
| return; | |
| } | |
| var time = new Date().getTime(); | |
| if (!console.timeCounters) { | |
| console.timeCounters = {}; | |
| } | |
| var key = "KEY" + name.toString(); | |
| if (!reset && console.timeCounters[key]) { | |
| return; | |
| } | |
| console.timeCounters[key] = time; | |
| }; | |
| console.timeEnd = function (name) { | |
| var time = new Date().getTime(); | |
| if (!console.timeCounters) { | |
| return; | |
| } | |
| var key = "KEY" + name.toString(); | |
| var timeCounter = console.timeCounters[key]; | |
| var diff; | |
| if (timeCounter) { | |
| diff = time - timeCounter; | |
| var label = name + ": " + diff + "ms"; | |
| console.info(label); | |
| delete console.timeCounters[key]; | |
| } | |
| return diff; | |
| }; | |
| } | |
| /* | |
| Example usage of console.time | |
| */ | |
| function MyFunctionName () { | |
| console.time('MyFunctionName'); | |
| //... do what you need to do | |
| console.timeEnd('MyFunctionName'); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment