Created
June 19, 2012 02:11
-
-
Save phalkunz/2951935 to your computer and use it in GitHub Desktop.
Simple JS running time profiler
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
/** | |
* Profiling (running time). For dev purpose only. | |
*/ | |
(function() { | |
window._profiler = { | |
'profiles': [] | |
}; | |
_profiler.time = function(name) { | |
_profiler.profiles[name] = new Date(); | |
}; | |
_profiler.timeEnd = function(name) { | |
var diff = new Date() - _profiler.profiles[name]; | |
print(diff); | |
}; | |
var print = function (val) { | |
// Only print the timer when jsprofile is present in the url querystring | |
if(/jsprofile/.test(window.location.search)) { | |
alert(val); | |
// Self-rewrite so that from the second time it doesn't need check url string | |
print = function(val) { | |
alert(val); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment