Created
January 29, 2016 11:15
-
-
Save ivan-kleshnin/fcf6353242f3a4a462e7 to your computer and use it in GitHub Desktop.
Test HtmlParser2
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 {Parser} from "htmlparser2"; | |
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 Parser({ | |
onopentag: function (name, attrs) { | |
if (name == "p") { | |
pCount += 1; | |
} | |
}, | |
onend: function () { | |
console.log("Total pCount =", pCount); | |
setImmediate(final); | |
} | |
}, { | |
decodeEntities: true | |
} | |
); | |
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