简体   繁体   English

开发过程中的对象冻结

[英]Object.freeze during development

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? 是否可以在开发过程中验证它是否未被修改,但是可以避免生产期间的任何运行时成本?

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. 我们使用Object.freeze冻结路由器并为非生产环境路由对象,以确保这些对象的不变性。

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. 对于生产环境,建议在构建过程中使用诸如envify和uglify之类的工具来去除[非]生产专用代码,以提高性能。

We use if (process.env.NODE_ENV !== 'production') to wrap around Object.freeze() , so that you can use various tools to build the code for different environments: 我们使用if (process.env.NODE_ENV !== 'production')来包装Object.freeze() ,以便您可以使用各种工具来为不同的环境构建代码:

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 ). 他们这样做的原因是因为Object.freeze 当时的速度很慢 -在这一点上, Object.freeze的性能影响已经大大减轻了至少在V8中 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM