I have a lot of console.log() in my app. Most of them are in catch blocks, so I can clearly see what went wrong when developing. Some are there to log the current time so I can check function execution times.
When deploying to production, those console.log() will run in the client's devices. Can I just leave them as they are? Will they hurt performance/memory or may cause some exception or unwanted behaviors?
console. log by itself doesn't really impact performance in a way that you'll notice unless you bind it to a scroll / resize handler. These get called alot and if your browser has to send text to the console like 30/60x a second it can get ugly.
Now you are rest assured your console. log(…) will work properly on your local or staging server but will not output anything on your production server. Comments, suggestions or corrections are welcome.
console. log() causes memory leaks but only when the console is opened. Normally users do not open the console. So it is totally safe to print any huge objects to the console.
Writing to the logs in a React Native app works just like in the browser: use console. log , console.
From React Native docs:
Console.log statements
When running a bundled app, these statements can cause a big bottleneck in the JavaScript thread. This includes calls from debugging libraries such as redux-logger, so make sure to remove them before bundling.
So yeah.. I would remove them :)
The ones in your catch statements may be ok to leave in as they only fire if there's an issue (would rather grab more info on that than worry about the performance hit)
There's more performance tips on the react native docs here
Is this good practice to disable all console.log statements in your entire app?
Any side effects?
At top of file: App.js include:
// To assign console.log to nothing
if (!__DEV__) {
console.log = () => {};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With