AngularJS, as known, is a structural framework for dynamic Web Applications. This tool is very popular among the developers. Not surprising taking into account that AngularJS is fully extensible and can work with other libraries without an effort. The creators claim that the resulting environment is simple to work with and readable. But is it fast enough?
Unfortunately, not as fast as it was promised, so here is an AngularJS tutorial with 7 Tips for Making Your AngularJS Application Faster.
1. Algorithm Complexity and Free Space
To make your programs work faster you have to deal with the time complexity of the algorithms. In general, the bigger the number of comparisons within the program is, the slower it works. Look, what is better linear search or binary search? It is not good for you to use the linear search instead of the binary because the first one needs more comparisons for the same data.
What makes your program run slower is the lack of available space. Computers need free memory to run code. Large programs face it each time. The more memory your solution needs, the slower the program is. Don’t forget that, and make sure, you have enough free computer memory to launch software solutions without accidents.
2. Watching Functions
What should you do with the utilization of functions in watchers and bindings? If you don’t want to get in trouble you mustn’t bind anything strictly to a function or watch a function result in a direct way. In our experience, the function runs each time in a digest cycle, slowing down your application.
3. Calls and Garbage Collector
To avoid your program slipping in the mud, create variables that are definitely needed and use them reasonably. Scope your variables to optimize the work of the garbage collector. A solution runs faster if this tool frees used memory as soon as possible.
Another thing that you should remember about is calls within a loop. Avoid doing that if it’s not necessary.
We don’t recommend developers to use filters as they run two times in every digest cycle. First time when something is changed and second time to collect future changes. Furthermore, they don’t remove parts from the computer memory. They simply mask filtered items with CSS instead.
Take into account that arrays perform faster than the objects, and it’s better to use numeric access to non-numeric.
With a closer look, it becomes obvious that the objects with few properties work faster. With the special representation, which is available by default, it is a good idea to have a minimum number of properties.
6. Manual Testing
Do you still use manual testing? What for? Unless you use TDD in your work, each time the project launches you have to do a manual testing. AngularJS was designed in such a way that the developed code was always testable during the work. DI, ngMock are your best friends in this. There are also a few tools that might be useful.
The first one is Protractor. Unit tests are the basement for developing fully tested applications, but as the project becomes bigger, usage of the integration tests is more effective. Protractor is able to mimic the interaction with the user.
Protractor uses the Jasmine framework for writing tests and has a good API for descriptions of the various interaction scenarios. Among that, Protractor understands the internal organization of AngularJS, so you can forget about the problems with the $digest loops.
The second one is called Karma. This test runner was also designed by AngularJS developers. Using Karma you can perform tests every time the source files are changed. Karma is able to perform in several browsers at a time. Different tools may be aimed at Karma server for complete coverage of the actual usage scenarios.
Forget about the long nights in front of the computer with the Batarang.
Batarang is a Chrome extension to develop and debug AngularJS applications. You can use this tool to speed up your work.
With Batarang You Can:
- view models attached to Scope
- build dependency graphs
- analyze an application performance
As the number of custom directives and complex logic starts to grow, an application slows down. With the help of Batarang, it is easy to figure out which functions spend too much time in the call. Along with that, Batarang displays a watch tree that is indispensable in cases when you use a large number of watchers.
These 7 tips will help you develop your applications better. Now all the solutions will work faster and they will be optimized. Remember that you can prevent many problems just being attentive. Don’t underestimate the preparation and planning stages before you start writing your code.