Closed bmeurer closed 5 years ago
As discussed sidechannel, this means undom's existing promise of avoiding shared prototypes between documents. However, for the performance payoff, that's pretty workable.
Since jsdom and domino both use shared prototypes like this PR, I think the precedent is set and we can merge.
Previously this exported a single function
undom()
, which had all the classes and functions inside, and would create new classes (including new prototypes, constructors, etc. for those classes) everytime you create a new document, which is not very efficient to say the least. So the first step to improve the performance here was to move all the classes and functions out of theundom()
function (which I took the liberty to rename tocreateDocument()
now, as that seems to make more sense to me).The next step was to move the methods and accessors that were slapped onto the
Document
andElement
instances to the respective prototypes instead. Specifically we avoid installing the 'cssText' and 'className' accessors in the constructor, and instead have these accessors installed once on theElement.prototype
, which gave a huge performance boost.The execution time of the following micro-benchmark, which creates lot's of documents and adds a single HTMLUnknownElement to it, goes
from around 6690ms to 67ms, which is a solid 100x performance
improvement.
Note that this is a breaking change in the sense that prototypes will be shared by multiple documents. But this seems to be consistent with what other DOM libraries like jsdom and domino do.