Skip to content

Instantly share code, notes, and snippets.

@dhigginbotham
Last active August 29, 2015 14:06
Show Gist options
  • Save dhigginbotham/a4bad555fdfd60c74df1 to your computer and use it in GitHub Desktop.
Save dhigginbotham/a4bad555fdfd60c74df1 to your computer and use it in GitHub Desktop.
mockup perf tool
var perf = (function(root) {
var keys = [];
var i = -1;
// internal api
function setter() {
if (i > -1) {
keys[i].stop = new Date().getTime();
keys[i].dist = keys[i].stop - keys[i].start;
}
}
// public api to get own key
function getter() {
return (i > -1 ? keys[i] : false);
}
// public api get distance of
// time between start & stop
function distance() {
return keys[i].stop - keys[i].start;
}
// public api to expose current size of
// keys within scope
function size() {
return keys.length;
}
// shim, because lame browsers exist in
// the wild
function index(key) {
for (var i=0;i<keys.length;++i) {
if (keys[i].name === key) return i;
}
return -1;
}
return function (key) {
if (typeof key == 'undefined') return keys;
i = index(key);
// internal api
function build() {
keys.push({
name: key,
start: new Date().getTime(),
stop: null
});
}
// tracking fn, toggles from start/stop
function track() {
return (i > -1 ? setter() : build());
}
return {
start: track,
stop: track,
delay: distance,
key: getter
}
}
})(window);
var perf=function(){function d(){c>-1&&(b[c].stop=(new Date).getTime(),b[c].dist=b[c].stop-b[c].start)}function e(){return c>-1?b[c]:!1}function f(){return b[c].stop-b[c].start}function h(a){for(var c=0;c<b.length;++c)if(b[c].name===a)return c;return-1}var b=[],c=-1;return function(a){function g(){b.push({name:a,start:(new Date).getTime(),stop:null})}function i(){return c>-1?d():g()}return"undefined"==typeof a?b:(c=h(a),{start:i,stop:i,delay:f,key:e})}}(window);
@dhigginbotham
Copy link
Author

perf('someTestName').start();
perf('someTestName').stop();
perf('someTestName').delay();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment