/ngPerf

Angular performance hint tool

Primary LanguageJavaScriptMIT LicenseMIT

ngPerf

AngularJS performance hint tool.

Background

  • Use one-time binding as possible
  • $scope.$apply() will spread from $rootScope among ALL scopes
  • $timeout() will call $rootScope.$apply(), it's expensive

Introduction

Overview

ngPerf is based on $decorator in AngularJS and provides console logs when Expensive method calling founded.

What is Expensive?

AngularJS uses $digest() as a dirty value check loop. While running a $digest() loop, AngularJS will run corresponding $$watchers to check if value has changed - it means the more $watcher you have, the slower a $digest loop will be.

In AngularJS 1.3, we are able to use binding like {{ ::exp }} to create a one-time-binding. It means that when the exp evaluating get stable, AngularJS will auto unwatch this exp and destroy the $watch on it. This will speed up the $digest loop.

Calling $apply() explictly will force AngularJS run a $digest loop, this is always useful when ngModel changes outside the angular context and seems OK. But, $apply() will bubble to $rootScope and cause a top to bottom $digest from $rootScope to every $scope in your application. In most situation what you want is just a $currentScope.$digest().

Improper $timeout usage, also, will delegate to $rootScope.$digest(). As far as I know, some people are writing $timeout(function(){}, 0) as a shortcut for $scope.$digest().

Usage

Basic

var ng_perf = new ngPerf('a.module.name')
ng_perf.setup()

Advanced

var mod = angular.module('a.module')

var options = {
  oneTime: true,
  explictApply: true,
  timeout: true
}

var ng_perf = new ngPerf(['a.module.name', mod], options)

Options

ngPerf(modules[, options])

  • modules: An array of String or angular.module
  • options:
    • oneTime: dig out potencial on-time-binding, default true
    • explictApply: monitering explict $apply calling, default true
    • timeout: monitering improper $timeout

ngPerf.prototype.setup()

  • Enable ngPerf monitering

ngPerf.setupWatherMonitor()

  • Enable watcher count monitor

ngPerf.setupScopeMonitor()

  • Enable scope count monitor

License

The MIT License (MIT)

Copyright (c) 2015 Yuwei Ba i@xiaoba.me

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Join the chat at https://gitter.im/ibigbug/ngPerf