<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Petro Karpiuk on Medium]]></title>
        <description><![CDATA[Stories by Petro Karpiuk on Medium]]></description>
        <link>https://medium.com/@petrokarpiuk?source=rss-f6a0198fc8c1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZAfXRadZGp7YwN6lQz8h_A.jpeg</url>
            <title>Stories by Petro Karpiuk on Medium</title>
            <link>https://medium.com/@petrokarpiuk?source=rss-f6a0198fc8c1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:34:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@petrokarpiuk/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Improve AngularJS performance by disconnecting scopes]]></title>
            <link>https://medium.com/@petrokarpiuk/improve-angularjs-performance-by-disconnecting-scopes-ab8fa1339067?source=rss-f6a0198fc8c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab8fa1339067</guid>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Petro Karpiuk]]></dc:creator>
            <pubDate>Mon, 22 Jan 2018 14:44:43 GMT</pubDate>
            <atom:updated>2018-09-15T15:22:30.241Z</atom:updated>
            <content:encoded><![CDATA[<p>There’s a simple trick that will make your AngularJS applications perform better.</p><p>The idea is borrowed from the Ionic framework and it will allow us to disconnect selected scopes, so the next digest iteration will not trigger their watchers.</p><h4>How it works:</h4><ul><li>Angular creates a hierarchy of $scope objects, with the $rootScope at the top.</li><li>Child scopes are connected and <em>implement</em> a doubly-linked list data structure. They keep references to the previous and next scopes in the list.</li><li>Then the change happens, <strong>$digest digest executes all the watchers in the </strong><strong>$scope</strong>, checks if the value needs to be updated. This is an expensive operation.</li><li>Then the change happens, $digest executes all the watchers in the $scope checks if the value needs to be updated. This is very expensive.</li><li>We can <strong>disconnect heavy</strong><strong>$scope, to reduce the number of </strong><strong>$$watchers being fired on the next $digest iteration</strong><em>.</em></li></ul><p>We only need two functions to do all of that.</p><blockquote>Implementation taken from <a href="https://github.com/ionic-team/ionic-v1/blob/master/js/utils/utils.js">Ionic</a>.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ff2e1fc9c1e3a1250089364b31b67ef/href">https://medium.com/media/2ff2e1fc9c1e3a1250089364b31b67ef/href</a></iframe><p>In the code above, we need to call disconnectScope with $scope we want to disconnect as an argument: disconnectScope($scopeToBeDisconnected) .</p><p>Next $digest run will omit watchers on the disconnected scope, since that scope, is no longer in the list.</p><p>We can use build-in <a href="https://docs.angularjs.org/api/ngMock/type/$rootScope.Scope#$countWatchers">$rootScope.countWatchers</a> method to track how the number of watchers is changing after disconnection scopes.</p><h4>Use cases</h4><p>The best use case for this technique is components with hidden content: tabs, accordions, wizard, pagination, etc.</p><p>Take tabs as an example, we don’t always want to trigger watchers on inactive tabs. Having complex components it can take a lot of time. We can simply disconnect scopes of the hidden components, and reconnect them back when the tab becomes active again.</p><p><strong>Notes</strong>:</p><ul><li>Disconnected $scope will not receive any broadcasted or emitted events.</li><li>After reconnecting, scope will become a tail node in a list.</li><li>In some cases, after reconnecting, we might need to run $digest manually to sync scopes.Improve AngularJS performance by disconnecting scopes</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab8fa1339067" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>