-
Notifications
You must be signed in to change notification settings - Fork 0
/
async-javascript-ch1sec1.2pg6.js
135 lines (112 loc) · 2.9 KB
/
async-javascript-ch1sec1.2pg6.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* The code in this file is based on code from the book Async JavaScript
* by Trevor Burnham. The original version can be found at ch1sec1.2pg6.
* This version does a bit more, but what is being tested is basically the
* same. It counts how many times you can get your callback to fire if you
* keep creating an async event using the same async mechanism.
*
* I could have run the tests with promises, but I think I prefer what I have
* here for now. Since the purpose is to test async mechanisms that
* JavaScript provides, using something that doesn't use any of the same
* async mechanisms to test those very mechanisms seems more correct to me.
*/
var fireCount = 0;
var start = new Date;
var testDuration = 1000;
function runTests(suites, suiteDone){
var sLen = suites.length;
while(sLen && typeof suites[0] !== 'function'){
suiteDone(suites.shift());
sLen--;
}
if(sLen === 0){ return; }
(suites.shift())(function(result){
suiteDone(result);
runTests(suites, suiteDone);
});
}
console.log('Tested | Nr. of Runs | Time Elapsed (ms)');
runTests(
[
typeof(requestAnimationFrame) !== 'undefined' ?
testAnimationFrame :
['requestAnimationFrame', 0],
testInterval,
testTimeout,
typeof(setImmediate) !== 'undefined' ?
testImmediate :
['setImmediate', 0],
testPromise,
typeof(global) !== 'undefined' ?
testTick :
['nextTick', 0]
], function(result){
console.log('%s | %d | %d', result[0], result[1], new Date - start);
start = new Date;
fireCount = 0;
}
);
function testInterval(done){
var timer = setInterval(function() {
if (new Date - start < testDuration) {
fireCount++;
} else {
clearInterval(timer);
done(['setInterval', fireCount]);
}
}, 0);
}
function testTick(done){
if(new Date - start < testDuration){
fireCount++;
process.nextTick(function(){ // async recursion
testTick(done);
});
} else {
done(['nextTick', fireCount]);
}
}
function testImmediate(done){
setImmediate(function(){
if (new Date - start < testDuration) {
fireCount++;
testImmediate(done); // async recursion
} else {
done(['setImmediate', fireCount]);
}
});
}
function testTimeout(done){
setTimeout(function(){
if (new Date - start < testDuration) {
fireCount++;
testTimeout(done); // async recursion
} else {
done(['setTimeout', fireCount]);
}
}, 0);
}
function testAnimationFrame(done){
if (new Date - start < testDuration) {
fireCount++;
requestAnimationFrame(function(timestamp){
testAnimationFrame(done); // async recursion
});
} else {
done(['requestAnimationFrame', fireCount]);
}
}
function testPromise(done){
var promise = Promise.resolve(fireCount);
promise.then(function me(value){
value++;
if(new Date - start < testDuration){
promise = promise.then(me);
} else {
promise = promise.then(function(value){
done(['Promise', value]);
});
}
return value;
});
}