1 define([ 'util/ensureCallback' ], function (ensureCallback) {
2 var requestAnimationFrame
3 = window.requestAnimationFrame
4 || window.webkitRequestAnimationFrame
5 || window.mozRequestAnimationFrame
6 || window.oRequestAnimationFrame
7 || window.msRequestAnimationFrame;
9 // Benchmarks fn until maxTime ms has passed. Returns an object:
12 // "score" -- approximate number of operations performed in that time ('score')
14 function benchAsync(maxTime, fn, callback) {
15 if (typeof fn !== 'function') {
16 throw new TypeError('Argument must be a function');
22 function checkDone(endTime) {
27 if (endTime - startTime >= maxTime) {
30 var elapsed = endTime - startTime;
31 var timeoutScore = timeoutTimes.length / elapsed * maxTime;
32 var rafScore = rafTimes.length / elapsed * maxTime;
33 return callback(null, {
35 timeoutScore: timeoutScore,
37 score: requestAnimationFrame ? rafScore : timeoutScore,
43 function rafUpdate() {
48 requestAnimationFrame(rafUpdate);
52 var timeoutTimes = [ ];
56 fn(timeoutTimes.length, function () {
58 timeoutTimes.push(now);
66 setTimeout(function () {
67 startTime = Date.now();
69 if (requestAnimationFrame) {
70 requestAnimationFrame(rafUpdate);