[AngularJS] 5 simple ways to speed up your AngularJS application
Nowdays, Single page apps are becoming increasingly popular among the fornt-end developers. It is the time to say goodbye with refreshing the whole page due to clicking on a single link. It helps to speed up out web application and improve our use experience. Today we'd talk about how to speed up your AngularJS application. This article is based on AngularJS 1.3.x version.
1. Disabling the debug info
When you use directive, Angular adds some additional debug information to the DOM.
Javascript:
app.controller('AppController', function () {
this.appCtrl = this; appCtrl.message = "Hello World!";
});
HTML:
<p>{{appCtrl.message}}</p>
Once compiled, we get something like this:
<p class="ng-binding">Hello World!</p>
This debug info is super useful when you debuggin because the tools like Protractor and Batarang which can rely on this information. However, great things always come with cost. When our project comes to production, those information is useless to users. And additional properties and classes that are added to the DOM also come with a performance cost depending on how much is stored on the scope and DOM operations are expensive anyway.
AngularJS 1.3 enables us to switch those debug information, what you need to do to disable the debug info:
app.config(function( $compileProvider ) { $compileProvider.debugInfoEnabled(false);
});
To enable it again, you can do in console:
angular.reloadWithDebugInfo();
2. Use $applyAsync
Configure $http service to combine processing of multiple http responses received at around the same time via $rootScope.$applyAsync. This can result in significant performance improvement for bigger applications that make many HTTP requests concurrently (common during application bootstrap) [1].
Sinlge $http request triggers $digest() which can help to update application model and DOM. If you have many $http requests triggered almost at the same time and each request will run $digest() which cost a lot.
The idea to use $applyAsync is to compile multiple $http requets into one $digest. We show how to use this here:
app.config(function($httpProvider) { $httpProvider.useApplyAsync(true);
});
3. Using one time binding
AngularJS comes up with two way binding which is cool because you don't need to set up any event listen, it helps you to update the model automaticlly. But, as we said, great thing comes with cost. In order to make databinding possible, Angular uses $watch APIs to observe model mutations on the scope. Imaging if there is too many watcher, your application become heavy and slow. This is why we need one-time binding. One-time expressions will stop recalculating once they are stable, which happens after the first digest.
You can see the demo here: http://jsbin.com/dijeno/9/edit?html,css,js,output
The way to use one time binding is quite simple, from our first simple of AppController, we know that everytime appCtrl.message changes, {{appCtrl.message}} will also change. So if we apply the one-time expression to our example above, we change this:
<p>{{::appCtrl.message}}</p>
4. Using lazy loading
Lazy loading is very useful when you have a large application which needs to load hundred files. And among those hundred files, there is only 10% files whcih are needed to bootstrap your applcation and the other 90% are only useful in the running time. So, in order to speed up, you don't want those 90% files loaded when the boostrap time. That is the place where lazy loading comes in to handy.
There are many ways to do lazy loading, for example RequireJS, the one which I often use is called ocLazyLoad. It is quite easy to use with AngularJS.
What you need to do first is install ocLazyLoad:
bower install oclazyload
Then inject into the application dependency:
var app = angular.module("demo", ["oc.lazyLoad"])
Javascript: When you click on the AppCtrl, we want to load everything related to the store module.
app.controller("AppCtrl", function($ocLazyLoad) { var app = this;
app.click = function() {
$ocLazyLoad.load({
name: "store",
files: [
"app/store/store.js",
"app/store/store.tpl.html",
"app/css/store.css"
]
})
}
})
5. Resolve data beforehand
Let see two parts of code first, then talk about why resolve is good.
Controller Way:
function MainCtrl(SomeService) { var mainCtrl= this; // unresolved
mainCtrl.something; // resolved asynchronously
SomeService.doSomething().then(function(response) {
mainCtrl.something = response;
}); } angular.module('app', [])
.controller('MainCtrl', MainCtrl);
Route Resolve:
// config with resolve pointing to relevant controller
function config ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl',
controllerAs: 'main',
resolve: MainCtrl.resolve
});
}
// controller as usual
function MainCtrl (SomeService) {
// resolved!
this.something = SomeService.something;
}
// create the resolved property
MainCtrl.resolve = {
doSomething: function (SomeService) {
return SomeService.doSomething();
}
}; angular
.module('app')
.controller('MainCtrl', MainCtrl)
.config(config);
For controller activate:
- Views load right away
- The Service code executes after the controller code
- Animation can be shown during the view transition
For route resolve
- The code executes before the route via a promise
- Resolve makes the new view wait for the route to resolve
- Animation can be shown before the resolve and through the view transition
Resolve happens before your controller will instantiate, and your template will load and everything will set up. It gives users a felling that they can wait less time to get the interface updated, because all the data are already resolved (parpared) for interface to use.
[AngularJS] 5 simple ways to speed up your AngularJS application的更多相关文章
- AngularJS进阶(三十三)书海拾贝之简介AngularJS中使用factory和service的方法
简介AngularJS中使用factory和service的方法 AngularJS支持使用服务的体系结构"关注点分离"的概念.服务是JavaScript函数,并负责只做一个特定的 ...
- 我的angularjs源码学习之旅1——初识angularjs
angular诞生有好几年光景了,有Google公司的支持版本更新还是比较快,从一开始就是一个热门技术,但是本人近期才开始接触到.只能感慨自己学习起点有点晚了.只能是加倍努力赶上技术前线. 因为有分析 ...
- angularJS 报错: [ngModel:numfmt] http://errors.angularjs.org/1.4.1/ngModel/numfmt?p0=333
<!doctype html> <html ng-app="a10086"> <head> <meta charset="utf ...
- angularjs工程流程走不通的原因以及使用angularjs流程注意点
1 入口index.html 在这个页面中要引入一些js,也就是说无论哪个模块下的js以及css都是在index.html下引入的,而在其他非index.html的html页面中,只有div模块代码, ...
- AngularJs中Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.15/
我在使用angularjs的时候报出来这个错误: Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.15/ 当时 ...
- Five ways to maximize Java NIO and NIO.2--reference
Java NIO -- the New Input/Output API package-- was introduced with J2SE 1.4 in 2002. Java NIO's purp ...
- Five ways to maximize Java NIO and NIO.2--转
原文地址:http://www.javaworld.com/article/2078654/java-se/java-se-five-ways-to-maximize-java-nio-and-nio ...
- AngularJS从构建项目开始
AngularJS从构建项目开始 AngularJS体验式编程系列文章,将介绍如何用angularjs构建一个强大的web前端系统.angularjs是由Google团队开发的一款非常优秀web前端框 ...
- 前端MVC学习总结(三)——AngularJS服务、路由、内置API、jQueryLite
一.服务 AngularJS功能最基本的组件之一是服务(Service).服务为你的应用提供基于任务的功能.服务可以被视为重复使用的执行一个或多个相关任务的代码块. AngularJS服务是单例对象, ...
随机推荐
- 『重构--改善既有代码的设计』读书笔记----Introduce Foreign Method
当你无法获得一个类的源代码或者没有权限去修改这个类的时候,你对于这种为你服务的类,你可能会出现需要别的需求的时候,比如一个Date类,你需要能够让他本身直接返回出他的后一天的对象,但他没有,这个时候你 ...
- ubuntu 升级命令
apt-get update && apt-get dist-upgrade
- centos6.5安装vsftp服务并配置虚拟账户ftp
当我们的用户量越来越大时,继续创建更多的系统用户是不明智的,这时就需要为vsftpd创建虚拟账户,但vsftpd虚拟账户的数据库要保存在Berkeley DB格式的数据文件中,所以需要安装db4- ...
- js中的潜伏者之Arguments对象
argument 说明: 在JavaScript中,arguments是对象的一个特殊属性.arguments对象就像数组,但是它却不是数组.可以理解为他是潜伏者,通俗的说,就是你传的参数不一定按照参 ...
- 自定义Excel导出简易组件
1.组件原理 excel的数据存储是以xml格式存储的,所以导出Excel文件可以通过生成XML来实现.当然XML必须符合一定的格式要求. 2.组件实现 (1)新建类库文件“MyExcel” (2)添 ...
- YII 登陆时 session持久化
在YII框架中,session持久化方法只需要调用login()方法就行了 class loginAction extends CAction{ function run(){ $model=new ...
- 局部变量存储区域静态变量存储区域static变量存储区域
局部变量存储区域静态变量存储区域static变量存储区域 常见的存储区域可分为: 1.栈 由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区.里面的变量通常是局部变量.函数参数等. 2.堆 ...
- Android 动画效果 及 自定义动画
1. View动画-透明动画效果2. View动画-旋转动画效果3. View动画-移动动画效果4. View动画-缩放动画效果5. View动画-动画效果混合6. View动画-动画效果侦听7. 自 ...
- WinPcap编程(一)
0. 按着文档顺序写的. 开发环境:win10+VS2013. 配置WinPcap环境就不多说.直接给网址:http://blog.sina.com.cn/s/blog_57432f380101qh3 ...
- github Travis CI 持续集成
一个项目如何保证代码质量是开发中非常重要的环节,对于开源项目来说更是如此,因为开源项目要面对的是来自不同水平开发者提交的代码.所以围绕开源做持续集成(Continuous Integration)变得 ...