Permalink
Browse files

Add simple benchmark helper

  • Loading branch information...
1 parent e042696 commit 677ab6cc4f92ad6d9cc988801a4ccd8eddb474bb Juriy Zaytsev committed Oct 22, 2008
Showing with 42 additions and 0 deletions.
  1. +42 −0 benchmark.js
View
@@ -0,0 +1,42 @@
+// bench(function(){ $$('div'); }, 100);
+
+function bench(fn, numIterations) {
+
+ if (!fn) return 0;
+
+ var numBenchmarks = 15, results = [];
+
+ function _bench(fn, numIterations) {
+ var i = numIterations || 1, t = new Date();
+ while (i--) {
+ fn();
+ }
+ return new Date() - t;
+ }
+
+ while (numBenchmarks--) {
+ results.push(_bench(fn, numIterations));
+ }
+
+ var average = (function(){
+ var i = results.length, sum = 0;
+ while (i--) {
+ sum += results[i];
+ }
+ return sum / results.length;
+ })();
+
+ var median = (function(){
+ var idx = results.length / 2;
+ if (results.length % 2 === 0) {
+ return ((results[idx] + results[idx + 1]) / 2);
+ }
+ return results[Math.ceil(idx)];
+ })();
+
+ return {
+ average: average,
+ actual: results,
+ median: median
+ };
+};

0 comments on commit 677ab6c

Please sign in to comment.