Skip to content

perf: lazy-load heavy dependencies in hardhat-utils - #8201

Merged
schaable merged 4 commits into
NomicFoundation:mainfrom
tenderdeve:perf/lazy-load-hardhat-utils
May 3, 2026
Merged

schaable merged 4 commits into
NomicFoundation:mainfrom
tenderdeve:perf/lazy-load-hardhat-utils

Conversation

@tenderdeve

Copy link
Copy Markdown
Contributor

Summary

Convert top-level imports of 6 heavy dependencies to cached await import(), loaded only on first use:

Dependency File Why
ethereum-cryptography crypto.ts Crypto init overhead on import
fast-equals internal/lang.ts Only needed for deepEqual
rfdc internal/lang.ts Only needed for deepClone
@streamparser/json-node fs.ts Only for readJsonFileAsStream
json-stream-stringify fs.ts Only for writeJsonFileAsStream
env-paths internal/global-dir.ts Rarely called at bootstrap

Follows the cached await import() pattern already established by undici in request.ts in this same package. No public API changes — all consuming functions were already async.

Fixes #8162

Test plan

  • Run npx hardhat --help — should be noticeably faster
  • Run npx hardhat compile — crypto functions still work
  • Run npx hardhat test — all tests pass
  • Verify readJsonFileAsStream / writeJsonFileAsStream still function

Convert top-level imports of 6 heavy dependencies to cached dynamic
imports, loaded only when the consuming function is first called:

- ethereum-cryptography (crypto.ts) — crypto init overhead
- fast-equals (internal/lang.ts) — only needed for deepEqual
- rfdc (internal/lang.ts) — only needed for deepClone
- @streamparser/json-node (fs.ts) — only for readJsonFileAsStream
- json-stream-stringify (fs.ts) — only for writeJsonFileAsStream
- env-paths (internal/global-dir.ts) — rarely called at bootstrap

Follows the cached await import() pattern already used by undici
in this package. No public API changes.

Fixes NomicFoundation#8162
@changeset-bot

changeset-bot Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: da1f8ba

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@schaable schaable assigned schaable and unassigned kanej May 2, 2026
@schaable
schaable marked this pull request as ready for review May 3, 2026 00:33
Copilot AI review requested due to automatic review settings May 3, 2026 00:33

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@schaable schaable left a comment

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.

Looks good. I made some minor cleanup and restored the type imports, since they don't affect performance.

@schaable schaable added no changeset needed This PR doesn't require a changeset no docs needed This PR doesn't require links to documentation no peer bump needed and removed status:triaging labels May 3, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR improves Hardhat startup performance by lazy-loading several heavy dependencies in @nomicfoundation/hardhat-utils, deferring their initialization until the relevant utility functions are actually used.

Changes:

  • Replaces eager imports of heavy deps with await import() in crypto, fs streaming JSON helpers, deep clone/equality helpers, and global-dir path generation.
  • Updates request.ts’s Undici type aliasing and uses pipeline directly from node:stream/promises.
  • Minor import-style adjustments in synchronization.ts (node built-ins).

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/hardhat-utils/src/synchronization.ts Switches some node built-in imports (e.g., hostname) to more granular/default imports.
packages/hardhat-utils/src/request.ts Small Undici typing rename and uses pipeline directly; keeps Undici lazy-loading behavior.
packages/hardhat-utils/src/lang.ts Adapts deepClone/deepEqual to await new async internal helpers.
packages/hardhat-utils/src/internal/lang.ts Lazy-loads rfdc and fast-equals on first use and caches results.
packages/hardhat-utils/src/internal/global-dir.ts Lazy-loads env-paths and caches the default export.
packages/hardhat-utils/src/fs.ts Moves streaming JSON deps behind dynamic imports within the stream-based helpers.
packages/hardhat-utils/src/crypto.ts Lazy-loads ethereum-cryptography hashing implementations on first use.

Comment thread packages/hardhat-utils/src/crypto.ts Resolved
Comment thread packages/hardhat-utils/src/fs.ts Resolved
Comment thread packages/hardhat-utils/src/fs.ts Resolved
@schaable
schaable added this pull request to the merge queue May 3, 2026
Merged via the queue into NomicFoundation:main with commit a4014a4 May 3, 2026
267 checks passed
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

no changeset needed This PR doesn't require a changeset no docs needed This PR doesn't require links to documentation no peer bump needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lazy load slow dependencies in hardhat-utils.

4 participants