--- title: How Does the Development Mode Work? date: '2019-08-04' spoiler: Dead code elimination by convention. cta: 'react' --- If your JavaScript codebase is even moderately complex, **you probably have a way to bundle and run different code in development and production**. Bundling and running different code in development and production is powerful. In development mode, React includes many warnings that help you find problems before they lead to bugs. However, the code necessary to detect such mistakes often increases the bundle size and makes the app run slower. The slowdown is acceptable in development. In fact, running the code slower in development *might even be beneficial* because it partially compensates for the discrepancy between fast developer machines and an average consumer device. In production we don’t want to pay any of that cost. Hence, we omit these checks in production. How does that work? Let’s take a look. --- The exact way to run different code in development depends on your JavaScript build pipeline (and whether you have one). At Facebook it looks like this: ```jsx if (__DEV__) { doSomethingDev(); } else { doSomethingProd(); } ``` Here, `__DEV__` isn’t a real variable. It’s a constant that gets substituted when the modules are stitched together for the browser. The result looks like this: ```jsx // In development: if (true) { doSomethingDev(); // 👈 } else { doSomethingProd(); } // In production: if (false) { doSomethingDev(); } else { doSomethingProd(); // 👈 } ``` In production, you’d also run a minifier (for example, [terser](https://github.com/terser-js/terser)) on the code. Most JavaScript minifiers do a limited form of [dead code elimination](https://en.wikipedia.org/wiki/Dead_code_elimination), such as removing `if (false)` branches. So in production you’d only see: ```jsx // In production (after minification): doSomethingProd(); ``` *(Note that there are significant limits on how effective dead code elimination can be with mainstream JavaScript tools, but that’s a separate topic.)* While you might not be using a `__DEV__` magic constant, if you use a popular JavaScript bundler like webpack, there’s probably some other convention you can follow. For example, it’s common to express the same pattern like this: ```jsx if (process.env.NODE_ENV !== 'production') { doSomethingDev(); } else { doSomethingProd(); } ``` **That’s exactly the pattern used by libraries like [React](https://reactjs.org/docs/optimizing-performance.html#use-the-production-build) and [Vue](https://vuejs.org/v2/guide/deployment.html#Turn-on-Production-Mode) when you import them from npm using a bundler.** (Single-file `