HUGE performance issues when using zod mock data (faker js)
Opened this issue · 1 comments
For a while now my next.js app has been running incredibly slowly causing me a lot of frustration both in development and production.
I used next bundle analyzer to investigate this and found that faker-js was causing this.
I believe this is likely due to the imports not being localized properly inside the zod-mock package
(see https://fakerjs.dev/guide/localization.html#individual-localized-packages)
I have tried:
- Added
@faker-js/faker
and@anatine/zod-mock
tonext.config.js
transpilePackages
array andoptimizePackageImports
- Changed zod mock dependency to run only in dev mode for testing
Unfortunately this did not help so for now I am uninstalling @anatine/zod-mock
Please let me know if there is any other info I can provide to help solve this issue
This is a good point.
So the mocking app here goes through every function name inside Faker. So it does load up everything in faker.
It was meant to be a testing tool
I don't even think this should slow down a back-end service. It will raise the memory footprint a bit, but Faker isn't that huge.
What I see is that you are using it on the front end. I wouldn't even recommend using Faker there, to be honest. For anything you need to use, consider using Faker directly and only import the modules you need.
If you are using the new server side components and you are mocking on the back end and this is ending up in your bundled front end - I'd say this is an import issue around NextJS. I was a developer during the height of the PHP days, so I am a bit wary of mixing server and client code in a single file—separation of concerns and all that.
However, you are correct in your overall assessment. This package was always meant to be a shortcut helper for fast mocking, mainly in a testing environment. The nature of the tool should just keep up with faker updates in general based on the way it works. In order to be able to fake every possible type, you have to import all of Faker.
Plus, it walks through a zod object and then searches through every Faker function until it finds one that might match. There is no attempt at a fast search there either as that is not the target. I suppose that could be improved to add some slight performance increase.