Code Monkey home page Code Monkey logo

angular-watchers-readme's Introduction

$digest cycle and dirty-checking

Overview

We've talked about the digest cycle, but we don't know why it actually might slow down a bit. Let's take a look at why.

Objectives

  • Describe the $digest cycle
  • Describe $$watchers

$$watchers

Whenever we need to watch a model value (such as using $scope.$watch or {{ ctrl.value }}), Angular pushes an item into a property named $$watchers on our scope.

What is a $$watcher? A watcher stores the function that we use to get the latest value of the item (remember the first function we passed through to $scope.$watch?), the last known value (the oldValue that got passed to our callback), the callback, and also whether or not we're deep watching the collection.

When we run the digest cycle, Angular loops through all of our watchers and executes the first function, which returns the latest value of the item. It then compares (or deep checks) the last value and the new value, calling the callback when the values are different.

Imagine we have an ng-repeat repeating a list of people with five different expressions displayed in the element: name, email, phone, address, and nickname. When we have 1000 people, we have 1000 * 5 = 5000 watchers added to the $$watchers array. These are then all run when we run the digest cycle. Can you see how it gets so slow now?

It's important that we try to minimize our $$watchers count. Use $scope.$watch only when you need to, and try to avoid watching full objects, as it takes more time to compare them.

angular-watchers-readme's People

Contributors

gj avatar ipc103 avatar toddmotto avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.