queckezz/koa-views

Slow performance with large options object

matkl opened this issue · 8 comments

matkl commented

Stringifying large objects can be slow. Removing JSON.stringify() here makes my renders 20 ms faster.

debug('render `%s` with %s', paths.rel, JSON.stringify(state, bigIntReplacer()))

Hi, it's not recommend to use debug in production environment.

It's the limit of debug, do you have any idea to debug here without JSON.stringify ?

matkl commented

The problem here is that JSON.stringify is called before debug and therefore is called in production environment.

I'd suggest to remove the state from the debug arguments if passing the state without stringifying is not possible.

matkl commented

OK, I looked up the debug documentation and you can check if the debug target is enabled. Wrapping the call with

if (debug.enabled) { // do stuff... }

will fix the issue.

The problem here is that JSON.stringify is called before debug and therefore is called in production environment.

Yes, it is. It's because debug cannot handle with BigInt, so here we use a replacer hack in JSON.stringify.

OK, I looked up the debug documentation and you can check if the debug target is enabled. Wrapping the call with

if (debug.enabled) { // do stuff... }

will fix the issue.

debug is enabled only if here's a DEBUG environment, so you should not set this environment in production in most cases.

matkl commented

Of course I'm not enabling debug in production. The issue is about JSON.stringify(state, bigIntReplacer()) being called on every render in production mode.

Thanks, released via v6.2.3