Skip to content

Instantly share code, notes, and snippets.

@makotom
Last active April 20, 2019 16:53
Show Gist options
  • Save makotom/616a2c405ed2f2419bec6048410066be to your computer and use it in GitHub Desktop.
Save makotom/616a2c405ed2f2419bec6048410066be to your computer and use it in GitHub Desktop.
This is how to use jsdom repetitively without leaking memory
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);
@makotom
Copy link
Author

makotom commented Apr 20, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment