Skip to content

Instantly share code, notes, and snippets.

@ivan-kleshnin
Created January 29, 2016 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivan-kleshnin/bb571f51d47aaebee16a to your computer and use it in GitHub Desktop.
Save ivan-kleshnin/bb571f51d47aaebee16a to your computer and use it in GitHub Desktop.
Test Parse5
import Fs from "fs";
import Parse5 from "parse5";
import MemWatch from "memwatch-next";
import humanFormat from "human-format";
process.on("unhandledRejection", function (reason, p) {
throw reason;
});
let timeScale = new humanFormat.Scale({
seconds: 1,
minutes: 60,
hours: 3600,
});
let source = Fs.createReadStream("./huge.html");
let pCount = 0;
let parser = new Parse5.SAXParser();
parser.on("startTag", function (name, attrs) {
if (name == "p") {
pCount += 1;
}
});
parser.on("finish", function () {
console.log("Total pCount =", pCount);
setImmediate(final);
});
let maxUsage = 0;
let hd = new MemWatch.HeapDiff();
MemWatch.on("stats", function (stats) {
if (stats.current_base > maxUsage) {
maxUsage = stats.current_base;
}
});
let startDate = new Date();
console.log(`started`);
source.pipe(parser);
let final = function () {
let stopDate = new Date();
let deltaTime = humanFormat((stopDate - startDate) / 1000, {scale: timeScale});
console.log(`stopped (${deltaTime})`);
MemWatch.gc();
let diff = hd.end();
console.log("Mem before:", diff.before.size);
console.log("Mem after:", diff.after.size);
console.log("Mem max:", humanFormat(maxUsage).toLowerCase() + "b");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment