Reputation: 58682
Please see the code here
if (process.env.NODE_ENV !== 'production') {
if ('function' === typeof Object.freeze) {
Object.keys(self._routes).forEach(function freezeRoute(name) {
var route = self._routes[name];
Object.freeze(route.config);
Object.freeze(route.keys);
Object.freeze(route);
});
Object.freeze(self._routes);
}
}
why do freeze in non production mode. Is it to verify it is not being modified during development, but avoid any kind of runtime cost during production?
Upvotes: 1
Views: 286
Reputation: 159915
Yes, this is precisely the reason mentioned in the commit where this functionality was added:
We use
Object.freeze
to freeze the router and route objects for non-production environments to ensure the immutability of these objects.For production environments, it is recommended to use tools like envify along with uglify as part of your build process to strip out the [non-]production specific code for performance benefits.
We use
if (process.env.NODE_ENV !== 'production')
to wrap aroundObject.freeze()
, so that you can use various tools to build the code for different environments:
The reason they did this was because Object.freeze
was slow at the time - at this point the performance hit of Object.freeze
has been greatly mitigated (at least in V8).
Upvotes: 2