Production build
Hi. Why in this project huge checks doesn't wrap into if(process.env.NODE_ENV...)? I understand, them are very helpful in the development stage but useless in production. I guess some of them can also slow down application, like:
Object.keys(finalReducers).forEach(key => {
var reducer = finalReducers[key];
if (typeof reducer(undefined, { type: ActionTypes.INIT }) === 'undefined') {
throw new Error(
`Reducer "${key}" returned undefined during initialization. ` +
`If the state passed to the reducer is undefined, you must ` +
`explicitly return the initial state. The initial state may ` +
`not be undefined.`
);
}
var type = Math.random().toString(36).substring(7).split('').join('.');
if (typeof reducer(undefined, { type }) === 'undefined') {
throw new Error(
`Reducer "${key}" returned undefined when probed with a random type. ` +
`Don't try to handle ${ActionTypes.INIT} or other actions in "redux/*" ` +
`namespace. They are considered private. Instead, you must return the ` +
`current state for any unknown actions, unless it is undefined, ` +
`in which case you must return the initial state, regardless of the ` +
`action type. The initial state may not be undefined.`
);
}
});
I don't think these checks are going to significantly impact performance as they are only called in combineReducers during initialization, but I do think you are right that they could be put behind a dev flag. One benefit would be dead code elimination.
I guess some of them can also slow down application, like:
Not likely, because they execute just once.
If you are very concerned about them you can write your own combineReducers without checks:
export default function combineReducers(reducers) {
return function combination(state = {}, action) {
return Object.keys(reducers).reduce((nextState, key) => {
nextState[key] = reducers[key](state[key], action);
return nextState;
}, {});
}
}
When you use Redux you should understand that the functions provided to you are for convenience but you can totally customize them, and shouldn't feel awkward doing so. It's normal: you own your functions and code. Don't feel trapped in functions that ship with Redux.
Why in this project huge checks doesn't wrap into if(process.env.NODE_ENV...)
These checks actually throw exceptions. They're not just warnings. If they were wrapped in if (...), then production and development behavior would differ, which is bad. If developer misses a problem and the problem makes it into production, it will eventually throw (because sanity checks actually check for important mistakes that will cause errors in production), but in a different place. You'll have weird stack traces that you can't possibly reproduce in development. For these reasons, if some behavior throws, it has to throw both in development and production.