rexfordessilfie / nextwrappers

Reusable, composable middleware-like wrappers for Next.js App Router Route Handlers and Middleware.
MIT License
23 stars 0 forks source link

Downsides to using AsyncLocalStorage within nextjs? #21

Open dbrxnds opened 1 year ago

dbrxnds commented 1 year ago

Hi,

Apologies if this is not the place for this question.

After seeing next.js use AsyncLocalStorage I was wondering if it's a bad idea to implement something similar for your own custom data? Are there any known downsides to this that you know of?

It seem great for sharing pieces of data that you know you will always need all around your application.

rexfordessilfie commented 1 year ago

Hello @dbrxnds! No worries, happy to have the discussion here. I am not an expert, but I ran some benchmark scripts on two different routes, one with AsyncLocalStorage enabled and one without, a snapshot of the results for a simple route can be found here.

Short answer: there looks to be a slight performance hit from testing, but it looks to be very negligible for a small benchmark I have ran

I would recommend copying and adapting the benchmark script on your routes to see what the impact of AsyncLocalStorage is for your application, compared to alternate approaches (e.g like drilling down arguments).

Also I am working on a new library for making it easier to apply async local storage to any function. Maybe it could be helpful for you! Demo here.

dbrxnds commented 1 year ago

Hi @rexfordessilfie, I appreciate the response! I figured there would be some performance hit, but I imagine it could be worth it compared to having to pass the parameters on and on. We are still on next pages dir, but would love to try it out.