Skip to content

Instantly share code, notes, and snippets.

@srikumarks
Created September 18, 2011 09:54
Show Gist options
  • Save srikumarks/1224926 to your computer and use it in GitHub Desktop.
Save srikumarks/1224926 to your computer and use it in GitHub Desktop.
A useful javascript profiling function ...
// f(N) is run a few times, timed and some stats are
// returned as an object.
function timeit(f, N) {
var start, stop, dt;
var worst = 0, best = 1000 * 3600, mean = 0, sigma = 0;
var i, M = 7;
for (i = 0; i < M; ++i) {
start = Date.now();
f(N);
stop = Date.now();
dt = stop - start;
best = Math.min(best, dt);
worst = Math.max(worst, dt);
mean += dt;
sigma += dt * dt;
}
mean /= M;
sigma /= M;
return { best: best, worst: worst, mean: mean, spread: Math.sqrt(sigma - mean * mean) };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment