Skip to content

Commit fe7bc26

Browse files
committed
add process code
1 parent 01cda52 commit fe7bc26

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

04-process/07-byte-to-size.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env node
2+
3+
const unit = ['', 'K', 'M', 'G', 'T', 'P'];
4+
5+
function bytesToSize(input, precision = 3) {
6+
const index = Math.floor(Math.log(input) / Math.log(1024));
7+
return (input / Math.pow(1024, index)).toFixed(precision) + ' ' + unit[index] + 'B';
8+
}
9+
10+
function mem() {
11+
const m = process.memoryUsage();
12+
console.log(bytesToSize(m.rss));
13+
console.log(bytesToSize(m.heapTotal));
14+
console.log(bytesToSize(m.heapUsed));
15+
console.log(bytesToSize(m.external));
16+
console.log(bytesToSize(m.arrayBuffers));
17+
console.log();
18+
}
19+
20+
mem();
21+
/*
22+
const fs = require('fs');
23+
fs.readFile('../09-stream/big.file', (err, data) =>{
24+
mem();
25+
fs.writeFile('./tmp.data', data, ()=>{
26+
console.log('ok');
27+
});
28+
});
29+
*/

0 commit comments

Comments
 (0)