Title: DEV mode change detection - Show warning if object reference changes in subsequent check · Issue #27804 · angular/angular · GitHub
Open Graph Title: DEV mode change detection - Show warning if object reference changes in subsequent check · Issue #27804 · angular/angular
X Title: DEV mode change detection - Show warning if object reference changes in subsequent check · Issue #27804 · angular/angular
Description: 🚀 feature request Relevant Package This feature request is for @angular/core Description When Angular does change detection, it will re-render the view whenever a property binding changes. There is a slight issue with what Angular consid...
Open Graph Description: 🚀 feature request Relevant Package This feature request is for @angular/core Description When Angular does change detection, it will re-render the view whenever a property binding changes. There is...
X Description: 🚀 feature request Relevant Package This feature request is for @angular/core Description When Angular does change detection, it will re-render the view whenever a property binding changes. There is...
Opengraph URL: https://github.com/angular/angular/issues/27804
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"DEV mode change detection - Show warning if object reference changes in subsequent check","articleBody":"# 🚀 feature request\r\n\r\n### Relevant Package\r\nThis feature request is for `@angular/core`\r\n\r\n### Description\r\nWhen Angular does change detection, it will re-render the view whenever a property binding changes. There is a slight issue with _what_ Angular considers a \"change\". If an object reference changes to an object identical to the first one, Angular will update the view with this new object, even if the view ends up being identical to the previous one. This situation is easy for new developers to fall into, as it very easy to write a function like:\r\n```\r\ndivClasses() {\r\n return {\r\n red: this.isRed\r\n };\r\n}\r\n```\r\nand then bind it to an `[ngClass]` directive, which will remove and re-apply the `red` class every change detection call, because `divClasses()` returns a new object every time it is run.\r\n```\r\n\u003cdiv [ngClass]=\"divClasses()\">\u003c/div>\u003c!-- This div will be updated every time change detection is ran. -->\r\n```\r\n\r\nThis causes two big problems, the first being inefficient view rendering. Change detection only needs to update the view whenever the component's `isRed` value changes, but it ends up updating the view every single time change detection is run. This would slow down the page and negatively impact the end user's experience.\r\n\r\nThe less obvious problem is the possibility of change detection recursion. If the application has a `MutationObserver` somewhere, watching for DOM changes, the above function will cause it to run every time change detection is ran. If this `MutationObserver` also triggers a change detection check, the page will hang as the `divClasses()` function updates the view, triggering the `MutationObserver`, which triggers change detection again. This might not even directly be the developer's fault, as the `MutationObserver` could be from a 3rd party library.\r\n\r\nI recently spent a day and a half attempting to debug an application hanging in this way. Funnily enough, the problem was made worse by a 3rd party library which modified `console.log` to run change detection - which caused recursion to happen again. Ultimately, I suspect this scenario might be the cause of issues like https://github.com/valor-software/ngx-bootstrap/issues/3047, https://github.com/angular/angular/issues/20450, or https://github.com/angular/angular/issues/24614.\r\n\r\n### Describe the solution you'd like\r\nTo be clear, my proposed \"fix\" does *NOT* involve modifying Angular's change detection to deeply compare object references. Discussion on this bug: https://github.com/angular/angular/issues/24614 was ultimately tabled because the proposed fix involved deep equality checks, which would be inefficient and very time consuming to check larger objects.\r\n\r\nUltimately, the issue here seems to be with functions like my example `divClasses()`, which inadvertently abuse change detection to unnecessarily update the view. If there was some way to alert the developer that a function they've written was possibly faulty, that would probably be the way to go.\r\n\r\nCurrently, Angular displays warnings for change detection when the application is running in dev mode for a similar scenario. If change detection itself causes a property/function to change its output, Angular currently displays a warning about what happened. Could it be possible to display a similar warning for whenever a function returns a different object reference on subsequent change detection calls? A reference check would not be very expensive, and this may end up helping the application developer optimize their program.","author":{"url":"https://github.com/ibedard16","@type":"Person","name":"ibedard16"},"datePublished":"2018-12-21T19:04:20Z","url":"https://github.com/angular/angular/issues/27804"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f4c7f470-1e24-33d1-b418-fff80b85d2a9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA14:666CF:5122B:6EF97:6B045408 |
| html-safe-nonce | b4457e57cda1ce5c0d191830f11b04861e959508a21451ba5b9db2a186caa592 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTE0OjY2NkNGOjUxMjJCOjZFRjk3OjZCMDQ1NDA4IiwidmlzaXRvcl9pZCI6IjgxNDkwMTI1OTgzNTMwNTY3NzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c9bcf1b3f3cfc2938b88249bafb9af5d96212fb7926d4595dc1fd48a50c69ba6 |
| hovercard-subject-tag | issue:393560545 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/27804/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2430936502a1f45b05e07a4f8a543c8c9b51cab343b126f8fb3a22920d4c3ad3/angular/angular/issues/27804 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2430936502a1f45b05e07a4f8a543c8c9b51cab343b126f8fb3a22920d4c3ad3/angular/angular/issues/27804 |
| og:image:alt | 🚀 feature request Relevant Package This feature request is for @angular/core Description When Angular does change detection, it will re-render the view whenever a property binding changes. There is... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ibedard16 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 910ca0e260cfec6aff11407d8dc6a0d3a4cd381a2ec9ada56627b063426892a0 |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | e086f9b7e88322eb2b41885a6ad425b215737fa5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width