-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathreadFile.js
99 lines (89 loc) · 2.54 KB
/
readFile.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var fs = require('fs');
var now = require('performance-now');
var totalLines = 0;
var lines = [];
var names = [];
var firstNames = [];
var dupeNames = {};
var dateDonationCount = [];
var dateDonations = {};
fs.readFile('itcont.txt', 'utf8', (err, contents) => {
console.time('line count');
let t0 = now();
if (contents !== undefined) {
totalLines = contents.split('\n').length - 1;
}
console.log(totalLines);
let t1 = now();
console.timeEnd('line count');
console.log(
`Performance now line count timing: ` + (t1 - t0).toFixed(3) + `ms`,
);
console.time('names');
t0 = now();
if (contents !== undefined) {
lines = contents.split('\n');
lines.forEach(line => {
var name = line.split('|')[7];
names.push(name);
});
}
console.log(names[432]);
console.log(names[43243]);
t1 = now();
console.timeEnd('names');
console.log(`Performance now names timing: ` + (t1 - t0).toFixed(3) + `ms`);
console.time('most common first name');
t0 = now();
names.forEach(name => {
var firstHalfOfName = name.split(', ')[1];
if (firstHalfOfName !== undefined) {
firstHalfOfName.trim();
if (firstHalfOfName !== ' ' && firstHalfOfName.includes(' ')) {
firstName = firstHalfOfName.split(' ')[0];
firstName.trim();
firstNames.push(firstName);
} else {
firstNames.push(firstHalfOfName);
}
}
});
firstNames.forEach(x => {
dupeNames[x] = (dupeNames[x] || 0) + 1;
});
var sortedDupeNames = [];
sortedDupeNames = Object.entries(dupeNames);
sortedDupeNames.sort((a, b) => {
return b[1] - a[1];
});
console.log(sortedDupeNames[0]);
t1 = now();
console.timeEnd('most common first name');
console.log(
`Performance now first name timing: ` + (t1 - t0).toFixed(3) + `ms`,
);
console.time('total donations for each month');
t0 = now();
lines.forEach(line => {
var timestamp = line.split('|')[4].slice(0, 6);
var formattedTimestamp =
timestamp.slice(0, 4) + '-' + timestamp.slice(4, 6);
dateDonationCount.push(formattedTimestamp);
});
dateDonationCount.forEach(x => {
dateDonations[x] = (dateDonations[x] || 0) + 1;
});
logDateElements = (key, value, map) => {
console.log(
`Donations per month and year: ${value} and donation count ${key}`,
);
};
new Map(Object.entries(dateDonations)).forEach(logDateElements);
t1 = now();
console.timeEnd('total donations for each month');
console.log(
`Performance now donations per month timing: ` +
(t1 - t0).toFixed(3) +
`ms`,
);
});