-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain-es6.js
303 lines (258 loc) · 6.46 KB
/
main-es6.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import ReactDOMStream from "react-dom-stream/server";
import ReactDOM from "react-dom/server";
import RecursiveDivs from "./RecursiveDivs";
import stream from "stream";
import React from "react";
import PoliteStream from "./PoliteStream";
import multistream from "multistream";
class CountingWritable extends stream.Writable {
constructor(options) {
super(options);
this.count = 0;
this.callCount = 0;
}
_write(chunk, encoding, next) {
this.callCount++;
this.count += chunk.length;
next();
}
}
class MakeString extends stream.Writable {
constructor(options) {
super({decodeStrings: false});
this.buffer = "";
}
_write(chunk, encoding, next) {
this.buffer += chunk;
next();
}
}
class ReadWrite extends stream.Readable {
constructor(wrapped, options) {
super(options);
this.wrapped = wrapped;
this.callCount = 0;
this.wrapped.on("end", () => {
this.push(null);
})
this.wrapped.on("close", () => {
this.push(null);
})
this.wrapped.on("error", (e) => {
this.push(null);
})
}
_read(n) {
let chunk;
this.callCount++;
setImmediate(() => {
if(null === (chunk = this.wrapped.read(n))) {
this.push("");
} else {
this.push(chunk);
}
});
}
}
function * generateContent(size) {
for (let i = 0; i < size; i++) {
yield "a";
}
}
function * generateNestedContent(depth, breadth) {
if (1 === depth) {
yield * generateContent(breadth);
} else {
for (let i = 0; i < breadth; i++) {
yield * generateNestedContent(depth - 1, breadth);
}
}
}
function syncContent(size) {
let result = "";
for (let i = 0; i < size; i++) {
result += "a";
}
return result;
}
class IteratorReadable extends stream.Readable {
constructor(iterator, options) {
super(options);
this.iterator = iterator;
}
_read(n) {
let countRead = 0;
var done, value, buffer = "";
while ({done, value} = this.iterator.next()) {
if (done) {
this.push(buffer);
this.push(null);
return;
} else {
buffer += value;
if (buffer.length >= n) {
this.push(buffer);
return;
}
}
}
}
}
const iterations = 10;
const main = async () => {
try {
let stringTotals = { time: 0, size: 0, callCount: 0}, streamTotals = { time: 0, size: 0, callCount: 0, inputCallCount: 0};
for (let i = 0; i < iterations; i++) {
let {time, size, callCount, inputCallCount} = await runString();
streamTotals.time += time;
streamTotals.size += size;
streamTotals.callCount += callCount;
streamTotals.inputCallCount += inputCallCount;
// ({time, size, callCount} = await runString());
// stringTotals.time += time;
// stringTotals.size += size;
// stringTotals.callCount += callCount;
}
console.log("Stream");
console.log("time: " + Math.round(streamTotals.time / iterations));
console.log("size: " + Math.round(streamTotals.size / iterations));
console.log("call count: " + Math.round(streamTotals.callCount / iterations));
console.log("input call count: " + Math.round(streamTotals.inputCallCount / iterations));
// console.log("String");
// console.log("time: " + Math.round(stringTotals.time / iterations));
// console.log("size: " + Math.round(stringTotals.size / iterations));
// console.log("call count: " + Math.round(stringTotals.callCount / iterations));
} catch (e) {
console.log(e.stack);
}
}
const runNestedGenerator = () => {
const start = new Date();
// let buffer = "";
// for (let chunk of generateContent(100000)) {
// buffer += chunk;
// }
let input = new IteratorReadable(generateNestedContent(5, 10));
let output = new CountingWritable();
// output.write(buffer);
// output.end();
input.pipe(output);
return new Promise((resolve, reject) => {
output.on("finish", () => {
resolve({
time: (new Date() - start),
size: output.count,
callCount: output.callCount,
});
});
});
}
const runGenerator = () => {
const start = new Date();
// let buffer = "";
// for (let chunk of generateContent(100000)) {
// buffer += chunk;
// }
let input = new IteratorReadable(generateContent(1000000));
let output = new CountingWritable();
// output.write(buffer);
// output.end();
input.pipe(output);
return new Promise((resolve, reject) => {
output.on("finish", () => {
resolve({
time: (new Date() - start),
size: output.count,
callCount: output.callCount,
});
});
});
}
const runSync = () => {
const start = new Date();
let buffer = syncContent(1000000);
let output = new CountingWritable();
output.write(buffer);
output.end();
return new Promise((resolve, reject) => {
output.on("finish", () => {
resolve({
time: (new Date() - start),
size: output.count,
callCount: output.callCount,
});
});
});
}
const runStream = () => {
const start = new Date();
let input = new PoliteStream(1000000); // ReactDOMStream.renderToString(<RecursiveDivs depth={10} breadth={2}/>).setEncoding("utf8");
let output = new CountingWritable();
input.pipe(output);
// return new Promise((resolve, reject) => {
// let buffer = "";
// let callCount = 0;
// input.on("readable", () => {
// callCount++;
// var chunk = input.read(1000);
// if (null !== chunk) {
// buffer += chunk;
// }
// });
// input.on("end", () => {
// resolve({
// time: (new Date() - start),
// size: buffer.length,
// callCount,
// });
// })
// });
return new Promise((resolve, reject) => {
output.on("finish", () => {
resolve({
time: (new Date() - start),
size: output.count,
callCount: output.callCount,
inputCallCount: 0, //input.callCount,
});
});
});
}
const runString = () => {
const start = new Date();
let input = ReactDOM.renderToString(<RecursiveDivs depth={13} breadth={2}/>);
let output = new CountingWritable();
output.write(input);
output.end();
return new Promise((resolve, reject) => {
output.on("finish", () => {
resolve({
time: (new Date() - start),
size: output.count,
callCount: output.callCount,
});
});
});
}
const runAsync = () => {
const start = new Date();
let input = ReactDOMStream.renderToString(<RecursiveDivs depth={13} breadth={2}/>);
let output = new CountingWritable();
input.pipe(output);
return new Promise((resolve, reject) => {
output.on("finish", () => {
resolve({
time: (new Date() - start),
size: output.count,
callCount: output.callCount,
});
});
});
}
main();
// let chunk;
// console.log("reading out the component");
// output.on("readable", () => {
// let chunk = output.read(100);
// console.log(chunk);
// });