Removing console.log from React Native app

believe best practice is to wrap your debug code in statements such as...

if(__DEV__){
    console.log();
}

This way, it only runs when you're running within the packager or emulator. More info here... https://facebook.github.io/react-native/docs/performance#using-consolelog-statements


I know this question has already been answered, but just wanted to add my own two-bits. Returning null instead of {} is marginally faster since we don't need to create and allocate an empty object in memory.

if (!__DEV__)
{
   console.log = () => null
}

This is obviously extremely minimal but you can see the results below

// return empty object
console.log = () => {}
console.time()
for (var i=0; i<1000000; i++) console.log()
console.timeEnd()

// returning null
console.log = () => null
console.time()
for (var i=0; i<1000000; i++) console.log()
console.timeEnd()

Although it is more pronounced when tested elsewhere:

empty console function

Honestly, in the real world this probably will have no significant benefit just thought I would share.


Well, you can always do something like:

if (!__DEV__) {
  console.log = () => {};
}

So every console.log would be invalidated as soon as __DEV__ is not true.


Babel transpiler can remove console statements for you with the following plugin:

npm i babel-plugin-transform-remove-console --save-dev

Edit .babelrc:

{
  "env": {
    "production": {
      "plugins": ["transform-remove-console"]
    }
  }
}

And console statements are stripped out of your code.

source: https://hashnode.com/post/remove-consolelog-statements-in-production-in-react-react-native-apps-cj2rx8yj7003s2253er5a9ovw