Thursday, August 13, 2015

React.JS with Angular.JS : The combination to improve performance

AngularJS is great front end MV* framework. The most popular feature of AngularJS is its two-way data binding mechanism and this is the strongest point behind its lack of performance.

 Here I am talking about scenario where we need to bind huge amount of data using ng-repeat. To overcome this performance backlog ,we can use React.js which is product of Facebook and they are using in production.

In this article, we will see one very simple example of React.JS with AngularJS.  Have a look on following code.

<html ng-app="fasterAngular">
    <meta name="viewport" content="width=device-width" />

    <script data-require="angular.js@@" data-semver="1.3.0-beta.5" src=""></script>
    <script data-require="react@@*" data-semver="0.10.0" src=""></script>


        var ReactComponent = React.createClass({
            displayName: 'ReactComponent',
            render: function () {
                return React.DOM.div(null, this.props.framework);

        angular.module('fasterAngular', []).
          controller('mycontroller', ['$scope', function ($scope) {

          }]).directive('fastReact', function () {
              return {
                  restrict: 'E',
                  scope: {
                      framework: '='
                  link: function (scope, el, attrs) {

                        // Render React virtual DOM
                          ReactComponent({ framework: 'I am React DOM' }),



            <div ng-controller="mycontroller">

Here is output.

Saturday, August 8, 2015

Load external JS file in non-blocking mode

Loading external JavaScript file in blocking mode could cause bottleneck of application’s performance. Normal behavior of Browser is to wait until all JS file load the reason is, any JS code in any file may change the design or content.

Anyway, there is couple of ways to load external JS file asynchronously or non-blocking way. In this article, we will discuss on by one of them.
Using “defer”

defer attribute makes promise to browser. When we are using defer and browser consider this attribute as expected, we should not include any DOM processing JS file in this. The reason is browser will not wait until the file load.
The syntax is like this.

<script src="~/js/js1.js" defer></script>

When browser encounter “defer” it consider the script as parallel loadable object. Now , the question is when defer code will execute ?

In theory, it should happen after the DOM has completely loaded, shortly before the DOMContentLoaded event. In practice, it depends on the OS and browser, whether the script is cached, and what other scripts are doing at the time.
Using “async”

This attribute has introduced in HTML5. async is identical to defer, except that the script executes at the first opportunity after download (an optional onload attribute can be added to run a specific function). You can’t guarantee that scripts will execute in sequence, but they will have loaded by the time the window onload event fires.There’s support for async in Firefox 3.6, Opera 10.5, and the latest WebKit build, so it should appear in the next versions of Chrome and Safari. IE9 is yet to support async, but the IE team could easily add it as an alias for defer. You can use both async and defer to support all browsers — even IE4.Perhaps within a few months, we’ll finally have a native, non-blocking JavaScript loading method that works in all browsers.
Let us see the loading sequence of async. In this example we are loading four external JS files and the sequence is like this.

Please note than “Raphael-min.js” is loading asynchronously.  Here is screen of loading in reality.

We are seeing that “Raphael-min.js” is loading in second position but in sequence, it is in third position. Again, this sequence of loading for some trial, it may vary time to time.