Skip to content

Two easy performance wins - #166

Merged
orangejulius merged 2 commits into
masterfrom
performance
Sep 21, 2026
Merged

orangejulius merged 2 commits into
masterfrom
performance

Conversation

@orangejulius

Copy link
Copy Markdown
Member

Two easy performance wins that help all our importers:

  • Host require calls out of Document constructor. This is the smaller one but it saves about 1% of the time an importer spends on a single record based on my testing.
  • Avoid extra function bindings and string manipulation in addParent. This is the bigger one, my testing shows 5% speedup from this alone. We call addParent several times per Document, so simplifying here really speeds things up!

`addParent` is called quite a lot during the import process (about 10
times per record) and each time it does string manipulation to calculate
property names, and binds two newly created functions.

It turns out by not doing those two things we can save somewhere around
3-5% of the work the importer does per record in the main thread, so a
pretty nice gain basically for free.
These requires running on each new Document call just waste cycles, in
my testing it's about a 1% savings in main thread CPU time to hoist them
out.
@orangejulius
orangejulius merged commit 583b91a into master Sep 21, 2026
5 checks passed
@orangejulius
orangejulius deleted the performance branch September 21, 2026 14:28
Comment thread Document.js
this.addPostProcessingScript( require('./post/patch') );
// create a non-enumerable property for post-processing scripts,
// seeded with a copy of the defaults so it stays writable per document
Object.defineProperty( this, '_post', { writable: true, value: defaultPostProcessingScripts.slice() });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I'm surprised this is so much less expensive considering calls to require() are cached.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I don't think the required module itself is executed, parsed, or anything like that again (we'd notice and the change would be huge). But it makes sense there would be some machinery that runs every time you call require, no matter what.

The measured difference is pretty small, about 1 microsecond averaged over a few million documents. It's measurable but only just. Still, that's about 1% of the time spent on each document.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants