简体   繁体   English

angularjs性能最佳实践

[英]angularjs performance best practices

I take a style based on johnpapa style guide , and according to Miško presentation on Angularjs MTV meetup Best Practices at 49:44 我采用基于johnpapa风格指南的风格 ,并根据Miško在Angularjs MTV聚会最佳实践的演讲49:44

he say that performance depend on 2 thing : 他说性能取决于两件事:

  1. How many binding you have. 你有多少绑定。
  2. How expensive you getter. 你得到多贵。 (should fast) (应该快)

And i think that means simple and fast. 我认为这意味着简单快捷。 In my comprehension getter mean a method in a service. 在我的理解中,getter意味着服务中的方法。 But in my case a little bit complex. 但在我看来有点复杂。 So how make it simple? 那怎么简单呢? i guest it's not possible to change the algorithm? 我是客人,不可能改变算法? or just calling internal (private) function in a service? 或者只是在服务中调用内部(私有)功能? does it take effect? 它会生效吗?

So here is my question : 所以这是我的问题:

  1. Best practice to make getter on a method in service so will not cause memory leak. 对服务方法进行getter的最佳实践不会导致内存泄漏。

    I hope somebody can make an clear axample with the explanation 我希望有人可以用解释做出明确的例子

  2. How IIFE work ? IIFE如何运作?

    Can somebody explain it to me? 有人可以向我解释一下吗?

  3. How IIFE work on a function inside factory (method in service)? IIFE如何处理工厂内的功能(使用中的方法)?

    Is it remove the global variable too? 它也删除了全局变量吗? even if i use var, like when we return a factory? 即使我使用var,就像我们回工厂一样?

Your question (or rather multiple questions) is rather broad. 你的问题(或更确切地说是多个问题)相当广泛。 And you are conflating and combining multiple concepts here, but I will try to untangle you. 你在这里混淆和组合了多个概念,但我会试着解开你。

First , IIFE. 首先 ,IIFE。 This has nothing to do with Angular performance. 这与Angular性能无关。 IIFE is a way to namespace your global scope. IIFE是一种命名全局范围的方法。 So, instead of exposing functions on the global scope that could result in a conflict, the best practice is to use IIFE. 因此,最好的做法是使用IIFE,而不是在可能导致冲突的全局范围上公开函数。

var globalObj =
    (function(){
      var privateVar = 5;
      function doPrivateFn(x, y){}

      return {
        publicFn: function(x){ doPrivateFn(x, privateVar) };
      }
    })();

Now, the only thing exposed is to the global scope is globalObj , which has globalObj.publicFn() . 现在,唯一暴露的是全局范围是globalObj ,它具有globalObj.publicFn()

Second , IIFE are sometimes used when registering services or controllers with Angular instead of inline anonymous function: .factory("MySvc", function(){}) : 其次 ,在使用Angular而不是内联匿名函数注册服务或控制器时,有时会使用IIFE: .factory("MySvc", function(){})

(function(){
  angular.factory("MySvc", MySvcFactory);

  function MySvcFactory($http){
    // ...
  }
})();

All this does is hides the MySvc factory function MySvcFactory from the global scope. 所有这一切MySvcFactory从全局范围隐藏MySvc工厂函数MySvcFactory

Finally , the point about making getters fast is not about memory leak, but rather computational intensity of the getter function. 最后 ,关于快速获取getter的观点不是内存泄漏,而是getter函数的计算强度。

So, if you have a binding in a expression, like this: 所以,如果你在表达式中有绑定,就像这样:

<span>{{getCount()}}</span>

or this: 或这个:

<div ng-show="isShown(item)">{{item.prop}}</div>

The idea is to make these functions very fast, if not just a getter: 这个想法是让这些功能非常快,如果不是只是一个吸气剂:

$scope.isShown = function(item){
  return (item.a || item.b) && $scope.somethingElse;
}

And never do things like this: 永远不要做这样的事情:

<div ng-show="isUnique(item)">{{item}}</div>

where isUnique perfoms a for loop search: isUnique执行for循环搜索:

$scope.isUnique = function(item){
   for (var i = 0; i < list.length; i++){
     if (item === list[i]) return false;
   }
   return true;
}

because this function will run on every digest cycle, and sometime even multiple times. 因为这个函数将在每个摘要周期运行,有时甚至多次。 Instead, calculate uniqueness and cache the result somewhere, so that you could just return it. 相反,计算唯一性并将结果缓存到某处,以便您可以返回它。

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

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