-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
executable file
·75 lines (62 loc) · 1.59 KB
/
utils.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
'use strict';
const fs = require('fs');
function readInput(path, encoding = 'utf8') {
return new Promise((resolve, reject) => {
fs.readFile(path, encoding, (err, result) =>
err ? reject(err) : resolve(result)
);
});
}
function writeOutput(path, data) {
return new Promise((resolve, reject) => {
fs.writeFile(path, data, (err, result) =>
err ? reject(err) : resolve(result)
);
});
}
function getMemoryUsageMessage() {
const memoryUsage = process.memoryUsage();
const message = Object.keys(memoryUsage)
.map(key => (memoryUsage[key] / 1024 / 1024).toFixed(2))
.join(' :: ');
return [
'Memory Usage (MiB):',
'RSS :: Heap Total :: Heap Used :: External :: Array Buffers',
message,
].join('\n');
}
function elapsedTime(debug = 1) {
let start = process.hrtime.bigint();
const timer = {
log: message => {
if (debug) {
const diff = process.hrtime.bigint() - start;
console.debug([
message && `${message}:`,
`${(diff / 1000000n).toLocaleString('en-US')}.${diff % 1000000n} ms`,
].join(' '));
}
return timer;
},
reset: () => {
start = process.hrtime.bigint();
return timer;
}
};
return timer;
}
function parseLine(rawLine, stringToNumberArrayIndexes = []) {
const parsedLine = rawLine.split(' ');
return stringToNumberArrayIndexes.length
? parsedLine.map((value, i) =>
stringToNumberArrayIndexes.includes(i) ? +value : value
)
: parsedLine;
}
module.exports = {
readInput,
writeOutput,
getMemoryUsageMessage,
elapsedTime,
parseLine,
};