-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeTest.js
More file actions
86 lines (59 loc) · 1.63 KB
/
timeTest.js
File metadata and controls
86 lines (59 loc) · 1.63 KB
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
const fl = "t.txt";
const fs = require("fs");
const readline = require("readline");
const path = 't.txt'
function testfoo(lines){
const start= new Date().getTime();
function readLinesFromFile(path , lines){
fs.readFile(path, async (err, data) => {
if (err) throw err;
data = data.toString().split(/\r?\n/)
const end = data.length
const start = end - lines
data.slice(start , end)
console.log('data.slice(start , end)')
const en = new Date().getTime();
console.log(`firtway: ${en - start}ms`);
});
}
readLinesFromFile(path , lines)
}
const linesCounter = async (fileName , lines) => {
start = new Date().getTime();
let cStream = fs.createReadStream(fileName);
const arr = []
const linesCount = await new Promise((resolve, reject) => {
let rl = readline.createInterface(cStream);
let count = 0;
rl.on("line", function () {
count++;
});
rl.on("error", reject);
rl.on("close", function () {
resolve(count);
});
});
let resStream = fs.createReadStream(fileName);
return await new Promise((resolve, reject) => {
let rl_1 = readline.createInterface(resStream);
let start = 0;
rl_1.on("line", function (line) {
start++;
if (start > linesCount - lines) {
arr.push(line);
}
});
rl_1.on("close", function () {
resolve(arr);
});
});
};
async function foo(lines){
let start= new Date().getTime();
const data = await linesCounter(fl , lines)
console.log('data')
const end = new Date().getTime();
console.log(`SecondWay: ${end - start}ms`);
}
foo(50000)
testfoo(50000)