Skip to content

Instantly share code, notes, and snippets.

@victor-homyakov
Created January 8, 2024 12:53
Show Gist options
  • Save victor-homyakov/ac65fb7c5fd21c0a05c5a78486bc8549 to your computer and use it in GitHub Desktop.
Save victor-homyakov/ac65fb7c5fd21c0a05c5a78486bc8549 to your computer and use it in GitHub Desktop.
Sort perf-PID.map file generated by Node.js
const fs = require('node:fs');
const readline = require('node:readline');
const fileName = process.argv[2];
if (!fileName) {
console.error('Please specify map file name as the first argument');
process.exit(1);
}
function extractAddress(/*string*/line) {
return line.substring(0, line.indexOf(' '));
}
async function sortPerfMapFile(fileName) {
const fileStream = fs.createReadStream(fileName);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
const /*string[]*/lines = [];
for await (const line of rl) {
if (line) {
lines.push(line);
}
}
lines.sort((s1, s2) => {
const addr1 = extractAddress(s1);
const addr2 = extractAddress(s2);
if (addr1.length < addr2.length) {
return -1;
} else if (addr1.length === addr2.length) {
return addr1.localeCompare(addr2);
} else if (addr1.length > addr2.length) {
return 1;
}
});
for (const line of lines) {
process.stdout.write(line);
process.stdout.write('\n');
}
}
sortPerfMapFile(fileName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment