-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (59 loc) · 1.51 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
60
61
62
63
64
65
66
67
68
69
70
71
72
// const file = require('./file.csv')
import { readFile, createReadStream} from 'node:fs'
import readline from 'node:readline'
const old = () => {
console.time()
readFile('file.csv', 'utf-8', (err, data) => {
// console.log(data)
console.timeEnd()
})
}
// default: 1.691s
const newLine = () => {
console.time()
const readable = createReadStream('file.csv')
const rl = readline.createInterface({
input: readable,
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
crlfDelay: Infinity,
})
rl.on('line', (line) => {
// console.log(`Line from file: ${line}`);
})
rl.on('close', () => {
console.log('finished!')
console.timeEnd()
})
}
// default: 3.872s ==> 978.47ms Utilizando los slice.
const newV = () => {
console.time()
const readable = createReadStream('file.csv', { highWaterMark: 20 })
let newLineI = ''
let newLine = ''
readable.on('data', chunk => {
// console.log({data: chunk.toString()})
const chunky = chunk.toString()
let indexSlice = 0
for(let i = 0; i < chunky.length; i++){
if(chunky[i] === '\n'){
newLine += chunky.slice(indexSlice, i)
// newLine += chunky[i]
console.log(newLine)
newLine = ''
indexSlice = i
}
// else {
// newLine += chunky[i]
// }
}
})
readable.on('end', () => {
console.log('finished!')
console.timeEnd()
})
}
// old()
newV()
// newLine()