简体   繁体   English

面向对象的Javascript /变量声明/性能

[英]Object Orientated Javascript / Variable declarations / Performance

So I have a rather large object orientated javascript class, with about 120 functions (a lot of getters and setters). 因此,我有一个相当大的面向对象的javascript类,它具有大约120个函数(很多getter和setters)。 Some of these functions have variables that are basically constants. 其中一些函数的变量基本上是常量。

What I'm wondering, is should I declare these variables in a global scope of the object, so every time the function is run it doesn't have to re-declare the variable? 我想知道的是,是否应该在对象的全局范围内声明这些变量,因此每次运行该函数时都不必重新声明该变量?

An example function is below. 下面是一个示例函数。 this.displayContacts is run several times (and will always run within the object), so in this case, there's no point declaring the 'codes' object inside the function? this.displayContacts运行了几次(并将始终在对象内运行),因此在这种情况下,在函数内部声明“ codes”对象没有意义吗?

function orderObject() {

  this.displayContacts = function() {
    var codes  = {'02':'02','03':'03','07':'07','08':'08'};
       // do something with codes
  };

}

So, would this be better, performance wise? 那么,在性能方面会更好吗?

function orderObject() {
var codes  = {'02':'02','03':'03','07':'07','08':'08'};
  this.displayContacts = function() {
    // do something with codes.
  };

}

My other concern is that if I end up with a lot of global variables/objects inside the main orderObject, will that be MORE of a performance hit than simply re-declaring the variables each time? 我的另一个担心是,如果我最终在主orderObject中包含许多全局变量/对象,那么与每次简单地重新声明变量相比,这是否会对性能产生更大的影响?

absolutely. 绝对。

function MyClass() {
   this.somevar = ''; // instance scoped variable
};

MyClass.CODES = {'02':'02'...}; // 'Class' scoped variable; one instance for all objects

MyClass.prototype.instanceMethod = function(){
  // 'this' refers to the object *instance*
  // it can still use MyClass.CODES, but can also account for variables local to the class
}

CONSTANT is 'static' so to speak, in java-talk. 用Java语言可以说, CONSTANT是“静态的”。 If your codes are global to the class (and the rest of your application), you will save a lot of overhead this way -- only define the object once. 如果您的codes对于该类(以及您的应用程序的其余部分)是全局的,则可以通过这种方式节省大量开销-仅定义一次对象。 Note that you can have 'static' class-level methods as well, for those cases where the function doesn't need to operate on variables specific to an instance of the class. 注意,对于函数不需要对特定于该类实例的变量进行操作的情况,您也可以使用“静态”类级方法。

Unless your app is really beefy, performance optimization probably wont make it noticeably faster. 除非您的应用程序真的很强大,否则性能优化可能不会使其速度明显提高。 But that doesn't mean that OO design is not worth-while -- if you are going to use javascript in an object oriented way, its not too hard and never a bad idea to use good OO principals. 但这并不意味着OO设计不值得-如果您打算以面向对象的方式使用javascript,那么使用良好的OO原理并不是一件困难的事,也不是一个坏主意。

I would say that if you have something that you are using in multiple places that it should become a property of your object so that it doesn't have to be redeclared each time. 我要说的是,如果您要在多个地方使用某些东西,那么它应该成为对象的属性,这样就不必每次都重新声明它。 It would also help make the maintenance of the object easier if that constant has to change. 如果必须更改该常数,这还将有助于使对象的维护更加容易。 Then you are changing it only in one place and not having to hunt down all the locations where you used it. 然后,您只需要在一个地方进行更改,而不必寻找使用它的所有位置。

Don't repeat yourself. 不要重复自己。

Garbage collection in JavaScript depends on the browser, and most modern browsers handle it pretty well. JavaScript中的垃圾收集取决于浏览器,大多数现代浏览器都很好地处理了它。 If you go ahead and make these global, you might see a slight performance increase simply because it's not executing that line of code every time. 如果继续进行这些工作,并使其具有全局性,您可能会看到性能略有提高,这仅仅是因为它没有每次都执行该行代码。 However, I can't imagine any significant increase in performance by making these static properties on the class, but if they don't change, then it would make more sense. 但是,我无法想象通过在类上创建这些静态属性来显着提高性能,但是如果它们没有改变,那将更有意义。

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

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