angular vs angularjs | key differences, performance, and popularity
AngularJS was released in 2009 and quickly became popular for it's two-way data binding, MVC architecture, and code reusability.
When alternatives like React and Vue delivered the same advantages of AngularJS with better performance, the Angular team decided to completely rewrite the framework.
As a result, Angular 2 was released in 2016. It offered superior performance and improved on other pitfalls of the original.
Each subsequent release of Angular (4,5,6,7,8) has been mostly non-breaking incremental changes. For these reasons, "Angular" now refers to Angular 2+ and "AngularJS" the original.
Here are the key differences between Angular 2+ and AngularJS:
While using Angular 2+ without TypeScript is possible, the industry standard is to adopt TypeScript as it plays much better with the Angular ecosystem.
MVC vs Component Architecture
AngularJS adheres to the model, view, controller (MVC) software design pattern. Controllers are defined with $scope variables representing an underlying data model. This data model can be updated in both the view and the controller. The view is an HTML file which both displays and dynamically updates $scope variables.
Angular 2+ utilizes more of a component based architecture. Isolated pieces of functionality are defined in components. These components reference their own templates and stylesheets and exist in a hierarchy of other components.
Dependency Injection (DI)
Both AngularJS and Angular use dependency injection. DI allows you to share commonly used functionality across different controllers or components.
In AngularJS, dependencies are injected in controller functions, link functions, and directive definitions.
In Angular, constructor functions, providers, and declarations are used to manage these dependencies.
Angular 2+ features the Angular CLI: a command line interface for quickly generating Angular components, services, directives, etc. It comes with convenient commands for building your Angular project (compiling TypeScript files and other assets into vanilla js files that run in the browser). It also makes building your project for different environments easier and allows for things like linting, type checking, etc.
AngularJS doesn't have it's own CLI.
Angular is much faster than AngularJS. In fact, it's said that Angular can be more than 5X faster based on the design of your application.
Angular fought back with the release of Angular 2 (2016). Today, Angular remains one of the most popular frameworks for UI development.
While AngularJS is still used today, it's popularity has died in favor of more current options like Angular 2+, React, and Vue.
The problem with AngularJS
Performance is one of the biggest problems with the original AngularJS. This is due to the underlying "magic" behind what originally made AngularJS so popular.
To achieve two-way data binding, AngularJS relies on a digest cycle to keep views in sync with their underlying data models. It works by augmenting all event handlers (clicks, ajax, timeouts) with a process called "dirty checking". Each scoped variable is compared to it's previous value.
If something has changed, the watchers and templates are updated with the new value and the process runs again to see if anything else has changed. In this way, the view is constantly in sync with the data model.
The problem with the AngularJS digest cycle is it's unpredictable. As applications grow, the "checking" process becomes more intensive and can run infinitely with two way data flow.
Angular 2+ to the rescue
To address these issues, the Angular team rewrote the framework with flux architecture in mind. Specifically unidirectional data flow was fundamental to reengineering change detection in Angular.
Now the Angular framework is just as fast as alternatives. When compared to AngularJS, Angular can be more than 5X faster.
The Angular CLI also makes minifying production bundle sizes a breeze, keeping Angular light weight for production..
Advantages of Angular
Angular offers many advantages over the original AngularJS:
Angular is up to 5X faster than AngularJS. This is because of a superior diffing algorithm featuring unidirectional data flow and component based architecture.
Server side rendering
Angular offers extensions for rendering your application server side. This is huge for SEO as certain web crawlers can't always scrape async content.
As a framework, Angular makes it possible to develop applications that work on both browsers and native devices like iOS and Android.
The tooling provided by TypeScript and the NodeJS ecosystem can't be underestimated. Using the Angular CLI, you can quickly generate Angular components, services, directives etc. without having to manually copy / paste a bunch of boilerplate code.
Additionally, you can more easily build and deploy your project using the CLI.
Should I use Angular or AngularJS?
With the performance advantages, Angular may seem like the best bet moving forward. There is a substantial learning curve to understanding the NodeJS / TypeScript ecosystem and one of the few advantages of AngularJS is that it just runs in the browser.
Using AngularJS makes sense if you have a small application and don't want to bother with learning the ins and outs of NodeJS and TypeScript.
There are also many existing projects out there that already use AngularJS and migrating to newer versions may not justify the cost of learning and rewriting code.
Outside of these cases, adopting "Angular" over the original "AngularJS" is preferred moving forward.