Last active
April 20, 2019 16:53
-
-
Save makotom/616a2c405ed2f2419bec6048410066be to your computer and use it in GitHub Desktop.
This is how to use jsdom repetitively without leaking memory
This file contains 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
const jsdom = require('jsdom'); | |
const domBuilder = (iter) => { | |
new jsdom.JSDOM(`${iter}`); | |
if (iter % 1000 === 0) { | |
console.log(iter); // eslint-disable-line no-console | |
} | |
setImmediate(domBuilder.bind(null, iter + 1)); | |
}; | |
domBuilder(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This kind of thing occurs because jsdom silently schedules some procedure for a generated Window object at the next tick.
Therefore, calling
JSDOM()
multiple times within a single tick, like this, leaves heavy Window objects referenced from the task queue of Node.js and results in heap depletion eventually.