在javascript中创建太多对象会影响性能吗? [英] Does creating too many objects in javascript will affect the performance?

查看:526
本文介绍了在javascript中创建太多对象会影响性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序并使用JavaScript创建许多对象,在整个会话过程中或网站打开时都需要维护这些对象,并且使所有对象都引用单个全局对象。它会影响webb应用程序的性能吗?

I am developing a web app and creating many objects in JavaScript, which I need to maintain throughout the session or when the website is open and I am making all objects referencing to a single global object. Will it affect the performance of webb app?

var Obj1 = somethig;
var obj200 = something;
window.global.Obj1 = Obj1;
window.global.Obj200 = Obj200;


推荐答案

免责声明:我是Chakra的一名软件工程师Internet Explorer 9和更高版本中的Javascript引擎(来自18号楼,您好!)

Disclaimer: I'm a software engineer on the Chakra Javascript engine in Internet Explorer 9 and later (Hello from Building 18!)

简而言之:取决于-我们需要知道您要创建多少个对象,它们有多复杂(因为JavaScript没有类,但是有原型和实例),创建它们的频率以及脚本/程序是否会导致GC收集对象(并且GC运行并不正常)。

In short: "it depends" - we need to know how many objects you're creating, how complex they are (as JavaScript does not have classes, but prototypes and instances), how frequently you're creating them, and if your script/program would cause the GC to collect the objects (and GC runs aren't pretty).

一些提示:


  1. 如果要存储许多简单的数据对象,使用数组来利用运行时将进行的任何优化。并且,如果您使用的是数组,请确保所有元素都具有相同的基础类型(例如,不要将JavaScript对象与同一数组中的数字混合使用)。

  2. JavaScript被垃圾回收-这意味着它具有所有相关的缺点,包括在GC运行时暂停整个脚本执行。如果一次可以收集很多对象,则GC暂停将运行一段时间。

  3. 避免实例属性(即使用原型属性或构造函数属性),即:

错误:

for(var i = 0; i < 1000; i++ ) {
    var foo = { baz: function() { return 5; } };
    foo.bar();
}

好:

function Foo() { } // `Foo` constructor.

Foo.prototype.baz = function() { return 5; };

for(var i=0; i < 1000; i++ ) {
    var foo = new Foo();
    foo.bar();
}

还不错:

 function Foo() { } 

 Foo.baz = function(foo) { return 5; };

 for(var i=0; i < 1000; i++ ) {
    var foo = new Foo();
    Foo.bar( foo );
 }

至于代码示例,如果您在根范围内(称为全局,在浏览器中是窗口对象的别名),然后是 var 关键字具有创建属性的作用。因此:

As for your code example, if you're in the root scope (called global, which in browsers is aliased by the window object) then the var keyword has the effect of making a property. So this:

var Obj1 = somethig;
var obj200 = something;
window.Obj1 = Obj1; // there is no `window.global` object
window.Obj200 = Obj200;

...实际上没有做任何事情: var Obj1 window.Obj1 是同一件事。

...doesn't actually do anything: var Obj1 is the same thing as window.Obj1.

最后,提示:仅给构造函数提供 TitleCase 名称,否则为所有其他内容(变量,参数,局部变量等) lowerCase 名称。调用实例 Obj1 使我的脸抽搐。

Finally, a protip: only give Constructor functions TitleCase names, otherwise everything else (vars, parameters, locals, etc) lowerCase names. Calling an instance Obj1 made my face twitch.

与往常一样,黄金法则适用:过早优化是万恶之源-首先对您的代码进行概要分析,然后在对代码进行重大更改之前查看是否存在问题(而IE 11的F12工具非常适合检查内存和处理性能代码,顺便说一句-并不是我没有偏见!)。

As always, the golden rule applies: premature optimisation is the root of all evil - profile your code first to see if there's a problem before making significant changes to your code (and IE 11's F12 tools are great for inspecting the memory and processing performance of your code, btw - not that I'm unbiased!).

这篇关于在javascript中创建太多对象会影响性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆