Create a gist now

Instantly share code, notes, and snippets.

@nutbread /timing.js Secret
Last active Aug 29, 2015

Embed
Generic timing function for JavaScript
"use strict";
var timing = (function () {
// Find performance/now object/function
var perf = window.performance,
now;
if (perf) {
now = perf.now ||
perf.mozNow ||
perf.msNow ||
perf.oNow ||
perf.webkitNow;
if (now) {
// Timing function
return function () {
return now.call(perf);
};
}
}
// Fallback
perf = null;
now = null;
return function () {
return new Date().getTime();
};
})();
var t1, t2;
t1 = timing();
(function () {
// do some things
})
t2 = timing() - t1;
console.log("Timed " + t2 + " seconds");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment