aigoncharov/class-logger

Performance when disabled

ThomasGHenry opened this issue ยท 3 comments

This looks awesome! If I wanted this in place for development and test, turned off in production, and only flipped on and off via config, what's the impact to performance in production (while disabled)?

So, say the stuff is all there, it's just off, is there a performance hit? Is every method checking the config and choosing not to log at runtime, or is the injection just not performed in the first place? Or...?

Thanks!

@LogClass wraps your class with a Proxy. Proxies are pretty fast nowadays. Yet it's still going to look up the config metainformation, it's still going to build the message and pass it to the logger of your choice. But that's how most of the loggers work, right? You can change the logging level, but the logic is still there. Anyway, I wouldn't expect it to be a massive performance hit.
If you're not convinced with the argument you can wrap LogClass decorator with your own implementation that will disable it conditionally:

import { LogClass as LogClassOriginal, IClassLoggerConfig } from 'class-logger'

export const LogClass = (config: IClassLoggerConfig) => {
  if (process.env.NODE_ENV === 'production') { // It can be any condition you want
    return () => undefined // return an empty decorator placeholder
  }
  return LogClassOriginal(config)
} 

@LogClass()
class Test {}

This would prevent your class being wrapped with a Proxy, therefore no performance impact at all.

It would be great if the above info was part of README.md