napi-rs/memory-testing/util.js
2022-01-24 17:25:40 +08:00

23 lines
823 B
JavaScript

const { whiteBright, red, green, gray } = require('colorette')
const prettyBytes = require('pretty-bytes')
const { table } = require('table')
module.exports = function displayMemoryUsageFromNode(initialMemoryUsage) {
const finalMemoryUsage = process.memoryUsage()
const titles = Object.keys(initialMemoryUsage).map((k) => whiteBright(k))
const tableData = [titles]
const diffColumn = []
for (const [key, value] of Object.entries(initialMemoryUsage)) {
const diff = finalMemoryUsage[key] - value
const prettyDiff = prettyBytes(diff, { signed: true })
if (diff > 0) {
diffColumn.push(red(prettyDiff))
} else if (diff < 0) {
diffColumn.push(green(prettyDiff))
} else {
diffColumn.push(gray(prettyDiff))
}
}
tableData.push(diffColumn)
console.info(table(tableData))
}