Should use ChangeDetectionStrategy.OnPush for eveything?












2















As the title says: I'm working on a very big project and in few components I've used ChangeDetectionStrategy.OnPush to avoid bad performances. I was wondering, is it "good" to put in every component that strategy and, in case, use ChangeDetectionRef.detectChanges() to programmatically update the component when needed?



--



That's a small component I have in the app:



 <my-map
(updatedGeometry)="setUpdatedGeometry($event)"
[startGraphEdit]="elementToEdit" [startCut]="elementToCut"
[startCopy]="elementToCopy"
[updateGraph]="elementToUpdate"
[showElement]="elementToShow"
(selectedProfile)="setProfile($event)"
[reducedChange]="reducedChange"
(reduceComposer)="setReducedComposer($event)"
[labelsVisible]="labelsVisible"
(visibleComposer)="setVisibleComposer($event)"
[activateLayers]="activeLayers"
(curLayers)="setCurrentLayers($event)"
[loadExtent]="extentToLoad"
(extent)="setExtent($event)"
[updateZoom]="newZoom"
(curZoom)="setCurrentZoom($event)"
(curLon)="setCurrentLon($event)"
(curLat)="setCurrentLat($event)"
(poiNotesOffset)="setPoiNotesOffset($event)"
[cancelPoiNoteCreation]="visibleDetailPanel"
(poiNoteUpdatedPosition)="setPoiNoteUpdatedPosition($event)"
[updatePoiNotePosition]="poiNotesElementForUpdate"
[removePoiNoteElement]="poiNotesElementForDeletion"
[updatePoiNotes]="updatePoiNotes"
[projectCode]="prjCode"
(poiNote)="poiNote($event)"
[setPrecisionPointerValues]="precisionPointerValues"
(precisionPointerValues)="updatePrecisionPointer($event)"
(exploreToolArea)="setExploreToolArea($event)"
(extentArea)="setExtentArea($event)"
[exploreToolRadius]="exploreToolRadius"
(newExploreToolRadius)="setExploreToolRadius($event)"
[currEnvironment]="currEnvironment"
(elementSelected)="onElementClick($event)"
[setaClasses]="classes"
[height]="mapHeight"
[width]="mapWidth"
[offsetX]="mapOffsetX"
[offsetY]="mapOffsetY"
[geometriesToHighlight]="geometriesToHighlight"
[highlightLineElements]="lineElements"
(poiList)="setPoiList($event)">
</my-map>


the component has a lot of Input and Output, and also communicate with others component using Subjects and BehaviorSubjects.










share|improve this question





























    2















    As the title says: I'm working on a very big project and in few components I've used ChangeDetectionStrategy.OnPush to avoid bad performances. I was wondering, is it "good" to put in every component that strategy and, in case, use ChangeDetectionRef.detectChanges() to programmatically update the component when needed?



    --



    That's a small component I have in the app:



     <my-map
    (updatedGeometry)="setUpdatedGeometry($event)"
    [startGraphEdit]="elementToEdit" [startCut]="elementToCut"
    [startCopy]="elementToCopy"
    [updateGraph]="elementToUpdate"
    [showElement]="elementToShow"
    (selectedProfile)="setProfile($event)"
    [reducedChange]="reducedChange"
    (reduceComposer)="setReducedComposer($event)"
    [labelsVisible]="labelsVisible"
    (visibleComposer)="setVisibleComposer($event)"
    [activateLayers]="activeLayers"
    (curLayers)="setCurrentLayers($event)"
    [loadExtent]="extentToLoad"
    (extent)="setExtent($event)"
    [updateZoom]="newZoom"
    (curZoom)="setCurrentZoom($event)"
    (curLon)="setCurrentLon($event)"
    (curLat)="setCurrentLat($event)"
    (poiNotesOffset)="setPoiNotesOffset($event)"
    [cancelPoiNoteCreation]="visibleDetailPanel"
    (poiNoteUpdatedPosition)="setPoiNoteUpdatedPosition($event)"
    [updatePoiNotePosition]="poiNotesElementForUpdate"
    [removePoiNoteElement]="poiNotesElementForDeletion"
    [updatePoiNotes]="updatePoiNotes"
    [projectCode]="prjCode"
    (poiNote)="poiNote($event)"
    [setPrecisionPointerValues]="precisionPointerValues"
    (precisionPointerValues)="updatePrecisionPointer($event)"
    (exploreToolArea)="setExploreToolArea($event)"
    (extentArea)="setExtentArea($event)"
    [exploreToolRadius]="exploreToolRadius"
    (newExploreToolRadius)="setExploreToolRadius($event)"
    [currEnvironment]="currEnvironment"
    (elementSelected)="onElementClick($event)"
    [setaClasses]="classes"
    [height]="mapHeight"
    [width]="mapWidth"
    [offsetX]="mapOffsetX"
    [offsetY]="mapOffsetY"
    [geometriesToHighlight]="geometriesToHighlight"
    [highlightLineElements]="lineElements"
    (poiList)="setPoiList($event)">
    </my-map>


    the component has a lot of Input and Output, and also communicate with others component using Subjects and BehaviorSubjects.










    share|improve this question



























      2












      2








      2


      1






      As the title says: I'm working on a very big project and in few components I've used ChangeDetectionStrategy.OnPush to avoid bad performances. I was wondering, is it "good" to put in every component that strategy and, in case, use ChangeDetectionRef.detectChanges() to programmatically update the component when needed?



      --



      That's a small component I have in the app:



       <my-map
      (updatedGeometry)="setUpdatedGeometry($event)"
      [startGraphEdit]="elementToEdit" [startCut]="elementToCut"
      [startCopy]="elementToCopy"
      [updateGraph]="elementToUpdate"
      [showElement]="elementToShow"
      (selectedProfile)="setProfile($event)"
      [reducedChange]="reducedChange"
      (reduceComposer)="setReducedComposer($event)"
      [labelsVisible]="labelsVisible"
      (visibleComposer)="setVisibleComposer($event)"
      [activateLayers]="activeLayers"
      (curLayers)="setCurrentLayers($event)"
      [loadExtent]="extentToLoad"
      (extent)="setExtent($event)"
      [updateZoom]="newZoom"
      (curZoom)="setCurrentZoom($event)"
      (curLon)="setCurrentLon($event)"
      (curLat)="setCurrentLat($event)"
      (poiNotesOffset)="setPoiNotesOffset($event)"
      [cancelPoiNoteCreation]="visibleDetailPanel"
      (poiNoteUpdatedPosition)="setPoiNoteUpdatedPosition($event)"
      [updatePoiNotePosition]="poiNotesElementForUpdate"
      [removePoiNoteElement]="poiNotesElementForDeletion"
      [updatePoiNotes]="updatePoiNotes"
      [projectCode]="prjCode"
      (poiNote)="poiNote($event)"
      [setPrecisionPointerValues]="precisionPointerValues"
      (precisionPointerValues)="updatePrecisionPointer($event)"
      (exploreToolArea)="setExploreToolArea($event)"
      (extentArea)="setExtentArea($event)"
      [exploreToolRadius]="exploreToolRadius"
      (newExploreToolRadius)="setExploreToolRadius($event)"
      [currEnvironment]="currEnvironment"
      (elementSelected)="onElementClick($event)"
      [setaClasses]="classes"
      [height]="mapHeight"
      [width]="mapWidth"
      [offsetX]="mapOffsetX"
      [offsetY]="mapOffsetY"
      [geometriesToHighlight]="geometriesToHighlight"
      [highlightLineElements]="lineElements"
      (poiList)="setPoiList($event)">
      </my-map>


      the component has a lot of Input and Output, and also communicate with others component using Subjects and BehaviorSubjects.










      share|improve this question
















      As the title says: I'm working on a very big project and in few components I've used ChangeDetectionStrategy.OnPush to avoid bad performances. I was wondering, is it "good" to put in every component that strategy and, in case, use ChangeDetectionRef.detectChanges() to programmatically update the component when needed?



      --



      That's a small component I have in the app:



       <my-map
      (updatedGeometry)="setUpdatedGeometry($event)"
      [startGraphEdit]="elementToEdit" [startCut]="elementToCut"
      [startCopy]="elementToCopy"
      [updateGraph]="elementToUpdate"
      [showElement]="elementToShow"
      (selectedProfile)="setProfile($event)"
      [reducedChange]="reducedChange"
      (reduceComposer)="setReducedComposer($event)"
      [labelsVisible]="labelsVisible"
      (visibleComposer)="setVisibleComposer($event)"
      [activateLayers]="activeLayers"
      (curLayers)="setCurrentLayers($event)"
      [loadExtent]="extentToLoad"
      (extent)="setExtent($event)"
      [updateZoom]="newZoom"
      (curZoom)="setCurrentZoom($event)"
      (curLon)="setCurrentLon($event)"
      (curLat)="setCurrentLat($event)"
      (poiNotesOffset)="setPoiNotesOffset($event)"
      [cancelPoiNoteCreation]="visibleDetailPanel"
      (poiNoteUpdatedPosition)="setPoiNoteUpdatedPosition($event)"
      [updatePoiNotePosition]="poiNotesElementForUpdate"
      [removePoiNoteElement]="poiNotesElementForDeletion"
      [updatePoiNotes]="updatePoiNotes"
      [projectCode]="prjCode"
      (poiNote)="poiNote($event)"
      [setPrecisionPointerValues]="precisionPointerValues"
      (precisionPointerValues)="updatePrecisionPointer($event)"
      (exploreToolArea)="setExploreToolArea($event)"
      (extentArea)="setExtentArea($event)"
      [exploreToolRadius]="exploreToolRadius"
      (newExploreToolRadius)="setExploreToolRadius($event)"
      [currEnvironment]="currEnvironment"
      (elementSelected)="onElementClick($event)"
      [setaClasses]="classes"
      [height]="mapHeight"
      [width]="mapWidth"
      [offsetX]="mapOffsetX"
      [offsetY]="mapOffsetY"
      [geometriesToHighlight]="geometriesToHighlight"
      [highlightLineElements]="lineElements"
      (poiList)="setPoiList($event)">
      </my-map>


      the component has a lot of Input and Output, and also communicate with others component using Subjects and BehaviorSubjects.







      angular






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 8:26







      Jacopo Sciampi

















      asked Nov 22 '18 at 8:10









      Jacopo SciampiJacopo Sciampi

      1,437117




      1,437117
























          2 Answers
          2






          active

          oldest

          votes


















          1














          ChangeDetectionStrategy.OnPush tells Angular that the component only depends on its @Inputs() and needs to be checked only in the following cases:



          The Input reference changes.



          An event originated from the component or one of its children.



          We run change detection explicitly.



          So it depends from your component's content and what you are trying to achieve with it. For example if you are using async pipe for your subscriptions, your component doesn't need ChangeDetectionStrategy.OnPush, because async will do the job automatically. If your component big and uses a lot of data changes, it should contain OnPush strategy, because it will increase your performance, so your whole component code will not run on every changes. If your component small and has only a few properties and methods, or it doesn't contain any subscription or @Input's, or doesn't do any data changes that will happen often, you don't need ChangeDetectionStrategy.OnPush






          share|improve this answer
























          • Thank you for the reply. I've edited my answer in order to help to understand better my scenario on this project.

            – Jacopo Sciampi
            Nov 22 '18 at 8:27











          • @JacopoSciampi if this is your small component, of course you must add OnPush strategy, otherwise when your project will rise bigger and bigger, you surely will have performance issues

            – Artyom Amiryan
            Nov 22 '18 at 8:30











          • Yep, the main container contains like about 25 others component with input and output, and the my-map is one of them. Thank you for your help.

            – Jacopo Sciampi
            Nov 22 '18 at 8:32



















          1














          NO NO and NO. Angular already introduces a lot of complexity to any code, but it gives you back a lot of features, such as change detection. If you remove change detection from Angular, then you are getting all the bad without the good. If you don't have thousands of components in your page, then you won't notice any perceptible improvements in removing change detection.



          So:




          • Always use change detection

          • Detach change detection in particular cases, where checking objects deeply can decrease performance in a considerable way






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426412%2fshould-use-changedetectionstrategy-onpush-for-eveything%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            ChangeDetectionStrategy.OnPush tells Angular that the component only depends on its @Inputs() and needs to be checked only in the following cases:



            The Input reference changes.



            An event originated from the component or one of its children.



            We run change detection explicitly.



            So it depends from your component's content and what you are trying to achieve with it. For example if you are using async pipe for your subscriptions, your component doesn't need ChangeDetectionStrategy.OnPush, because async will do the job automatically. If your component big and uses a lot of data changes, it should contain OnPush strategy, because it will increase your performance, so your whole component code will not run on every changes. If your component small and has only a few properties and methods, or it doesn't contain any subscription or @Input's, or doesn't do any data changes that will happen often, you don't need ChangeDetectionStrategy.OnPush






            share|improve this answer
























            • Thank you for the reply. I've edited my answer in order to help to understand better my scenario on this project.

              – Jacopo Sciampi
              Nov 22 '18 at 8:27











            • @JacopoSciampi if this is your small component, of course you must add OnPush strategy, otherwise when your project will rise bigger and bigger, you surely will have performance issues

              – Artyom Amiryan
              Nov 22 '18 at 8:30











            • Yep, the main container contains like about 25 others component with input and output, and the my-map is one of them. Thank you for your help.

              – Jacopo Sciampi
              Nov 22 '18 at 8:32
















            1














            ChangeDetectionStrategy.OnPush tells Angular that the component only depends on its @Inputs() and needs to be checked only in the following cases:



            The Input reference changes.



            An event originated from the component or one of its children.



            We run change detection explicitly.



            So it depends from your component's content and what you are trying to achieve with it. For example if you are using async pipe for your subscriptions, your component doesn't need ChangeDetectionStrategy.OnPush, because async will do the job automatically. If your component big and uses a lot of data changes, it should contain OnPush strategy, because it will increase your performance, so your whole component code will not run on every changes. If your component small and has only a few properties and methods, or it doesn't contain any subscription or @Input's, or doesn't do any data changes that will happen often, you don't need ChangeDetectionStrategy.OnPush






            share|improve this answer
























            • Thank you for the reply. I've edited my answer in order to help to understand better my scenario on this project.

              – Jacopo Sciampi
              Nov 22 '18 at 8:27











            • @JacopoSciampi if this is your small component, of course you must add OnPush strategy, otherwise when your project will rise bigger and bigger, you surely will have performance issues

              – Artyom Amiryan
              Nov 22 '18 at 8:30











            • Yep, the main container contains like about 25 others component with input and output, and the my-map is one of them. Thank you for your help.

              – Jacopo Sciampi
              Nov 22 '18 at 8:32














            1












            1








            1







            ChangeDetectionStrategy.OnPush tells Angular that the component only depends on its @Inputs() and needs to be checked only in the following cases:



            The Input reference changes.



            An event originated from the component or one of its children.



            We run change detection explicitly.



            So it depends from your component's content and what you are trying to achieve with it. For example if you are using async pipe for your subscriptions, your component doesn't need ChangeDetectionStrategy.OnPush, because async will do the job automatically. If your component big and uses a lot of data changes, it should contain OnPush strategy, because it will increase your performance, so your whole component code will not run on every changes. If your component small and has only a few properties and methods, or it doesn't contain any subscription or @Input's, or doesn't do any data changes that will happen often, you don't need ChangeDetectionStrategy.OnPush






            share|improve this answer













            ChangeDetectionStrategy.OnPush tells Angular that the component only depends on its @Inputs() and needs to be checked only in the following cases:



            The Input reference changes.



            An event originated from the component or one of its children.



            We run change detection explicitly.



            So it depends from your component's content and what you are trying to achieve with it. For example if you are using async pipe for your subscriptions, your component doesn't need ChangeDetectionStrategy.OnPush, because async will do the job automatically. If your component big and uses a lot of data changes, it should contain OnPush strategy, because it will increase your performance, so your whole component code will not run on every changes. If your component small and has only a few properties and methods, or it doesn't contain any subscription or @Input's, or doesn't do any data changes that will happen often, you don't need ChangeDetectionStrategy.OnPush







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 8:22









            Artyom AmiryanArtyom Amiryan

            1,835113




            1,835113













            • Thank you for the reply. I've edited my answer in order to help to understand better my scenario on this project.

              – Jacopo Sciampi
              Nov 22 '18 at 8:27











            • @JacopoSciampi if this is your small component, of course you must add OnPush strategy, otherwise when your project will rise bigger and bigger, you surely will have performance issues

              – Artyom Amiryan
              Nov 22 '18 at 8:30











            • Yep, the main container contains like about 25 others component with input and output, and the my-map is one of them. Thank you for your help.

              – Jacopo Sciampi
              Nov 22 '18 at 8:32



















            • Thank you for the reply. I've edited my answer in order to help to understand better my scenario on this project.

              – Jacopo Sciampi
              Nov 22 '18 at 8:27











            • @JacopoSciampi if this is your small component, of course you must add OnPush strategy, otherwise when your project will rise bigger and bigger, you surely will have performance issues

              – Artyom Amiryan
              Nov 22 '18 at 8:30











            • Yep, the main container contains like about 25 others component with input and output, and the my-map is one of them. Thank you for your help.

              – Jacopo Sciampi
              Nov 22 '18 at 8:32

















            Thank you for the reply. I've edited my answer in order to help to understand better my scenario on this project.

            – Jacopo Sciampi
            Nov 22 '18 at 8:27





            Thank you for the reply. I've edited my answer in order to help to understand better my scenario on this project.

            – Jacopo Sciampi
            Nov 22 '18 at 8:27













            @JacopoSciampi if this is your small component, of course you must add OnPush strategy, otherwise when your project will rise bigger and bigger, you surely will have performance issues

            – Artyom Amiryan
            Nov 22 '18 at 8:30





            @JacopoSciampi if this is your small component, of course you must add OnPush strategy, otherwise when your project will rise bigger and bigger, you surely will have performance issues

            – Artyom Amiryan
            Nov 22 '18 at 8:30













            Yep, the main container contains like about 25 others component with input and output, and the my-map is one of them. Thank you for your help.

            – Jacopo Sciampi
            Nov 22 '18 at 8:32





            Yep, the main container contains like about 25 others component with input and output, and the my-map is one of them. Thank you for your help.

            – Jacopo Sciampi
            Nov 22 '18 at 8:32













            1














            NO NO and NO. Angular already introduces a lot of complexity to any code, but it gives you back a lot of features, such as change detection. If you remove change detection from Angular, then you are getting all the bad without the good. If you don't have thousands of components in your page, then you won't notice any perceptible improvements in removing change detection.



            So:




            • Always use change detection

            • Detach change detection in particular cases, where checking objects deeply can decrease performance in a considerable way






            share|improve this answer




























              1














              NO NO and NO. Angular already introduces a lot of complexity to any code, but it gives you back a lot of features, such as change detection. If you remove change detection from Angular, then you are getting all the bad without the good. If you don't have thousands of components in your page, then you won't notice any perceptible improvements in removing change detection.



              So:




              • Always use change detection

              • Detach change detection in particular cases, where checking objects deeply can decrease performance in a considerable way






              share|improve this answer


























                1












                1








                1







                NO NO and NO. Angular already introduces a lot of complexity to any code, but it gives you back a lot of features, such as change detection. If you remove change detection from Angular, then you are getting all the bad without the good. If you don't have thousands of components in your page, then you won't notice any perceptible improvements in removing change detection.



                So:




                • Always use change detection

                • Detach change detection in particular cases, where checking objects deeply can decrease performance in a considerable way






                share|improve this answer













                NO NO and NO. Angular already introduces a lot of complexity to any code, but it gives you back a lot of features, such as change detection. If you remove change detection from Angular, then you are getting all the bad without the good. If you don't have thousands of components in your page, then you won't notice any perceptible improvements in removing change detection.



                So:




                • Always use change detection

                • Detach change detection in particular cases, where checking objects deeply can decrease performance in a considerable way







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 8:34









                Cristian TraìnaCristian Traìna

                2,08611523




                2,08611523






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426412%2fshould-use-changedetectionstrategy-onpush-for-eveything%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'