Improve performance with multiple documents by up to 100x #24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously this exported a single function
undom()
, which had all theclasses 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 the
undom()
function (which I took theliberty to rename to
createDocument()
now, as that seems to make moresense to me).
The next step was to move the methods and accessors that were slapped
onto the
Document
andElement
instances to the respective prototypesinstead. Specifically we avoid installing the 'cssText' and 'className'
accessors in the constructor, and instead have these accessors installed
once on the
Element.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.