Created
January 29, 2016 11:15
-
-
Save ivan-kleshnin/bb571f51d47aaebee16a to your computer and use it in GitHub Desktop.
Test Parse5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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