-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (51 loc) · 1.61 KB
/
index.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
'use strict'
const { createReadStream, createWriteStream } = require('fs')
const { pipeline, PassThrough } = require('stream')
const {createGzip, createGunzip, createBrotliCompress, createBrotliDecompress, createDeflate, createDeflateRaw} = require('zlib')
const fs = createReadStream('input.txt')
const ws = createWriteStream('./output.txt')
class MonitorData extends PassThrough {
constructor() {
super()
this.totalSize = 0
}
_transform(chunk, enc, cb) {
this.totalSize += chunk.length
cb()
}
}
const algorithms = {
gzip: createGzip,
brotli: createBrotliCompress,
deflate: createDeflate
}
const compressAndMeasure = (algorithmName, algorithm) => {
const initiator = new PassThrough()
const monitor = new MonitorData()
fs.pipe(initiator)
const startTime = new Date().getTime()
return new Promise((resolve, reject) => {
pipeline(
initiator,
algorithm(),
monitor,
(err) => {
if(err) {
console.error(err)
process.exit()
}
const totalTime = new Date().getTime() - startTime
console.log(`${algorithmName} finished successfully, with total time of: ${totalTime} and total size of ${monitor.totalSize}`)
ws.write(`${algorithmName}: ${totalTime}ms, ${monitor.totalSize}\n`)
resolve()
}
)
})
}
const runAlgorithms = async () => {
for (const key in algorithms) {
await compressAndMeasure(key, algorithms[key])
}
ws.end()
}
runAlgorithms()