forked from kangax/protolicious
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.js
42 lines (34 loc) · 833 Bytes
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
};
};