Skip to content

Instantly share code, notes, and snippets.

@krutoo
Created March 6, 2024 11:19
Show Gist options
  • Save krutoo/bdf26d6e48b7c4aa811c5567b16046ad to your computer and use it in GitHub Desktop.
Save krutoo/bdf26d6e48b7c4aa811c5567b16046ad to your computer and use it in GitHub Desktop.
Get Bun stats sorted diff
import fs from 'node:fs/promises';
const before = await readJson('./stats_before.json');
const after = await readJson('./stats_after.json');
getDiff(before, after)
.sort((a, b) => b[1] - a[1])
.forEach(([key, value]) => {
console.log(key, `+${value}`);
});
function getDiff(a, b, prefix = '') {
const result = [];
for (const key of Object.keys(a)) {
const beforeVal = a[key];
const afterVal = b[key];
if (typeof beforeVal === 'number') {
if (afterVal - beforeVal > 0) {
result.push([`${prefix}${key}`.padEnd(48, ' '), afterVal - beforeVal]);
}
}
if (beforeVal !== null && typeof beforeVal === 'object') {
result.push(...getDiff(beforeVal, afterVal, `${key}.`));
}
}
return result;
}
function readJson(filename) {
return fs.readFile(filename, 'utf-8').then(JSON.parse);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment