-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
basic.js
74 lines (58 loc) · 1.23 KB
/
basic.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
'use strict'
const bench = require('fastbench')
const syncthrough = require('../')
const through2 = require('through2')
const through = require('through')
const { PassThrough } = require('node:stream')
const data = Buffer.from('hello')
function write (i, s) {
for (; i < 1000; i++) {
if (!s.write(data)) {
i++
break
}
}
if (i < 1000) {
s.once('drain', write.bind(undefined, i, s))
} else {
s.end()
}
}
function benchThrough2 (done) {
const stream = through2()
stream.on('data', noop)
stream.on('end', done)
write(0, stream)
}
function benchPassThrough (done) {
const stream = PassThrough()
stream.on('data', noop)
stream.on('end', done)
write(0, stream)
}
let lastDone = null
function benchThrough (done) {
const stream = through()
lastDone = done
stream.on('data', noop)
stream.on('end', next)
write(0, stream)
}
function next () {
// needed to avoid a max stack call exception
process.nextTick(lastDone)
}
function benchSyncThrough (done) {
const stream = syncthrough()
stream.on('data', noop)
stream.on('end', done)
write(0, stream)
}
function noop () {}
const run = bench([
benchThrough2,
benchThrough,
benchPassThrough,
benchSyncThrough
], 10000)
run(run)