-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreams.js
executable file
·39 lines (30 loc) · 1.12 KB
/
streams.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
#!/usr/bin/env node
const fs = require('fs');
const highland = require('highland');
const { performance, PerformanceObserver } = require('perf_hooks');
const obs = new PerformanceObserver((list, observer) => {
console.log('Operation completed after: ', list.getEntries()[0].duration.toFixed(2), 'ms');
performance.clearMarks();
observer.disconnect();
});
obs.observe({ entryTypes: ['measure'], buffered: true });
performance.mark('Start');
const output = fs.createWriteStream(__dirname + '/outputStreams.csv');
const data = highland(fs.createReadStream(__dirname + '/data.csv', 'utf8'))
.split()
.map(row => row.split('|'))
.filter(row => row[2])
.filter(row => row[4])
.map(row => {
const formattedPhone = row[4].replace(/[\sx()-.]/g, '');
row[4] = formattedPhone;
const outputRow = row.join();
return `${outputRow.slice(0, outputRow.length - 1)}\n`;
})
.pipe(output);
output.on('finish', () => {
const used = process.memoryUsage().heapUsed / 1024 / 1024;
console.log('Memory used: ', parseInt(used, 10), 'MB');
performance.mark('End');
performance.measure('Completed', 'Start', 'End');
});