Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoize url.resolve #324

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

winkler1
Copy link

@winkler1 winkler1 commented Oct 27, 2020

url.resolve is called once per schema, and once for every field in the schema.

URL parsing contributes heavily to execution time, and is unnecessary for simple schemas. I've created a file memoizedResolve.js to cache results of the last call.

Benchmarking shows the following for 10,000 invocations:

284ms with caching
484ms without caching: return func.apply(null, arguments); in memoizedResolve.js

Additionally, cache hits will allocate fewer objects and create less GC pressure.

@winkler1 winkler1 force-pushed the memoize-uri-resolve branch 3 times, most recently from 1625472 to 61ae9f4 Compare October 28, 2020 10:16
@winkler1
Copy link
Author

winkler1 commented Nov 2, 2020

@awwright Any thoughts? The test is just there as a benchmark to demonstrate how much of a hotspot url.resolve is :)

@awwright
Copy link
Collaborator

awwright commented Nov 2, 2020

This seems reasonable.

I'll play around with it a bit... I don't know why it would be so expensive, the majority of calls should be trivial to compute (where the URI reference is blank).

Also, for the next major version, I was going to pre-compute all of the values before validation.

Why:

* Resolve is an expensive API call

This change addresses the need by:

* Cache results of last evaluation.
@awwright awwright added the bug label Feb 12, 2021
@awwright awwright added this to the v1.5 milestone Feb 12, 2021
@awwright
Copy link
Collaborator

I just saw this updated... I'm tagging it 'bug' because it can go out very next release, rather than being a feature enhancement that would bump the minor version number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants