简体   繁体   English

Javascript 对象重量/大小影响性能

[英]Javascript Object Weight/Size affect performance

In My project I usually I prefix all my JS object with my project name like if my project is XXX I will have all my lib as XXX.search , XXX.popup , XXX.login , XXX.login.sso etc.. so that all my object comes under XXX .在我的项目中,我通常在我的所有 JS 对象XXX.search加上我的项目名称,就像我的项目是XXX我将所有的 lib 都作为XXX.searchXXX.popupXXX.loginXXX.login.sso等等。这样我所有的对象都在XXX下。 Basically I am loading my XXX object with lot of modules.基本上我正在用很多模块加载我的XXX对象。 So the xxx object become huge.所以xxx对象变得巨大。

Will it affect JS performance in any case.无论如何都会影响JS性能。

What is good practice, having every module separate or having all the modules under my object.什么是好的做法,将每个模块分开或将所有模块都放在我的对象下。

Or it doesn't matter both gives same performance effect?或者两者都提供相同的性能效果并不重要?

Implementing namespacing with Object s is a pretty common JavaScript pattern.使用Object实现命名空间是一种非常常见的 JavaScript 模式。 Nearly everything is an object in JavaScript, so I don't see how using Object literals to namespace things would exact any significant extra costs.在 JavaScript 中几乎所有东西都是对象,所以我看不出使用对象字面量来命名事物会产生任何显着的额外成本。 Even if there is a significant cost to using objects as namespacing, I think that the benefit of working with clean code would outweigh it.即使使用对象作为命名空间的成本很高,我认为使用干净代码的好处会超过它。

Addy Osmani writes a lot about these topics. Addy Osmani 写了很多关于这些主题的文章。 I suggest you start his article on the subject if you're having trouble.如果你遇到问题,我建议你开始他关于这个主题的文章 Also keep in mind that there are many alternatives to objects as name spaces, like immediately invoked function expressions (though these incur a cost as well).还要记住,对象作为名称空间有很多替代方案,例如立即调用的函数表达式(尽管这些也会产生成本)。

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

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