Skip to content

refactor(module): reuse one Faker instance per FakerCore in standalone functions - #4260

Closed
TheNetherWatcher wants to merge 1 commit into
faker-js:nextfrom
TheNetherWatcher:perf/standalone-faker-cache
Closed

TheNetherWatcher wants to merge 1 commit into
faker-js:nextfrom
TheNetherWatcher:perf/standalone-faker-cache

Conversation

@TheNetherWatcher

Copy link
Copy Markdown

Since #4033 (not released yet), 17 standalone functions that resolve a pattern through helpers.fake build a new Faker on every call, e.g. fakePersonLastName, fakeLocationCity, fakeCompanyName, fakeInternetUserAgent. Building a Faker sets up every module, so these calls, and the module methods that call them (faker.person.lastName() etc.), are now roughly 10-40x slower than in 10.6.0.

This adds an internal getFakerForCore(fakerCore) that keeps one Faker per FakerCore in a WeakMap, and uses it in those 17 functions. The cached instance shares the core's randomizer, locale and config, so the output is unchanged for every seed. No snapshots changed. Seeded output of next and this branch matches across all 78 locales (20 methods that use these functions, 60 draws each).

The helper only gets imported by files that already imported Faker, so it adds no new import edges and doesn't conflict with #4220.

µs per call (best of 5 runs of 20k calls), Node 24.12, Apple M4 Pro:

10.6.0 next this PR
person.lastName() 0.50 19.91 0.53
person.fullName() 1.62 21.21 1.64
person.jobTitle() 1.23 20.46 1.16
location.city() 0.94 23.53 0.93
location.streetAddress() 2.17 45.85 2.11
company.name() 1.92 59.47 1.84
commerce.productName() 1.26 19.84 1.21
internet.userAgent() 3.06 21.75 2.89
hacker.phrase() 2.66 21.26 2.54

…e functions

Standalone functions that resolve patterns via helpers.fake created a
new Faker on every call. Reuse one instance per FakerCore instead; all
state lives in the FakerCore, so the output is unchanged.
@TheNetherWatcher
TheNetherWatcher requested a review from a team as a code owner November 6, 2026 16:43
@netlify

netlify Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for fakerjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 0bc7dcb
🔍 Latest deploy log https://app.netlify.com/projects/fakerjs/deploys/6aee03b74e02cd27a78c9021
😎 Deploy Preview https://deploy-preview-4260.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.14%. Comparing base (698d335) to head (0bc7dcb).

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #4260   +/-   ##
=======================================
  Coverage   99.14%   99.14%           
=======================================
  Files        1179     1180    +1     
  Lines        3976     3983    +7     
  Branches      709      724   +15     
=======================================
+ Hits         3942     3949    +7     
  Misses         29       29           
  Partials        5        5           
Files with missing lines Coverage Δ
src/internal/faker-for-core.ts 100.00% <100.00%> (ø)
src/modules/commerce/product-description.ts 100.00% <100.00%> (ø)
src/modules/commerce/product-name.ts 100.00% <100.00%> (ø)
src/modules/company/name.ts 100.00% <100.00%> (ø)
src/modules/finance/transaction-description.ts 100.00% <100.00%> (ø)
src/modules/food/description.ts 100.00% <100.00%> (ø)
src/modules/food/dish.ts 100.00% <ø> (ø)
src/modules/hacker/phrase.ts 100.00% <100.00%> (ø)
src/modules/internet/user-agent.ts 100.00% <100.00%> (ø)
src/modules/location/city.ts 100.00% <100.00%> (ø)
... and 8 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ST-DDT

ST-DDT commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Thanks for your interest in improving the performance of Faker.

The fake method calls will be replaced before v11.0 releases using resolver functions.

Which would render this PR's changes obsolete.

@ST-DDT ST-DDT closed this Nov 6, 2026
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