Load protodef-validator only when validation is requested - #174
Closed
u9g wants to merge 1 commit into
Closed
Conversation
require('protodef') was paying for protodef-validator and its ajv
dependency tree (~19ms warm, more cold) even for consumers that never
validate: the top-level require in protodef.js loaded it
unconditionally, and the module-scope ProtoDef() in index.js (which
only exists to export the default types) constructed a validator
eagerly on top.
Move the require into the constructor's validation branch and build the
types-export instance with validation off. Validation behavior when
requested is unchanged; the validator simply loads on first use.
Contributor
Author
|
19ms… |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
require('protodef')loadsprotodef-validatorand its ajv dependency tree for every consumer, even ones that never validate (e.g. node-minecraft-protocol's serializer usesnew ProtoDef(false)). Profilingrequire('mineflayer')with aModule._loadhook shows the validator+ajv subtree costs ~19ms warm — noticeably more with a cold file cache — out of a ~140ms warm require.Two eager load points:
src/protodef.jsrequires the validator at the top of the module, unconditionally.src/index.jsconstructs a module-scopenew ProtoDef()(validation defaults on) just to export the defaulttypes, so even the lazy require would still fire for everyone.Change
validation ? ... : nullbranch, so it loads on first validated construction.Validation behavior when requested is unchanged:
new ProtoDef(true)(the default) still constructs a working validator.Verification
npm test: 497 passing, standard clean.require('protodef')no longer hasprotodef-validatorinrequire.cache;new ProtoDef(true)still does.