Coder Social home page Coder Social logo

Comments (7)

JoostK avatar JoostK commented on May 3, 2024 2

This happens because the IntersectionObserver used to implement the on viewport trigger is asynchronous, so each level introduces its own microtick delay and therefore an additional round of change detection.

The documentation also states that the on viewport trigger is driven by IntersectionObserver, confirming its asynchronous behavior, so this isn't a bug per se. That said, I can see how a "preflight" viewport test might help avoid the additional microtick latency and its associated change detection cycle.

from angular.

JoostK avatar JoostK commented on May 3, 2024

Even that isn't great really, as it would result in a forced layout.

from angular.

olegdunkan avatar olegdunkan commented on May 3, 2024

I reopen the issue because I need some clarification from angular team.
Is it ok to execute template expressions O(n^2) times in the case of defered recursive template?

from angular.

alxhub avatar alxhub commented on May 3, 2024

I'm going to close this as working-as-intended.

In general, using @defer recursively this way will do N change detections as it's truly a waterfall of rendering:

  1. A @defer block is rendered and creates the placeholder element in the first change detection.
  2. IntersectionObserver asynchronously observes the placeholder in the layout and fires an event.
  3. That event triggers the @defer block, and the next round of change detection runs, creating the next @defer block (goto 1)

It's not feasible for Angular to synchronously evaluate the on viewport condition as that would require reflowing the page in the middle of rendering, which could have a significantly negative performance impact on the rest of the application.

Each template expression will be executed O(N) times - once for each step in the waterfall.

You observe your logging statement being called O(N^2) times because each instance of the template has its own logging binding, so you end up with N instances * O(N) calls per instance.

from angular.

olegdunkan avatar olegdunkan commented on May 3, 2024

In general, using @defer recursively this way will do N change detections as it's truly a waterfall of rendering

@alxhub May I conclude that using recursive template is anti-pattern in angular?

from angular.

alxhub avatar alxhub commented on May 3, 2024

Recursive templates aren't really related to this issue. They don't by themselves cause waterfalls, and it's easy to rewrite the example into a waterfall without recursive templates at all.

from angular.

olegdunkan avatar olegdunkan commented on May 3, 2024

@alxhub now I see, the problem is we can't render all ng-template embedded views in one changes detection round because IntersectionObserver triggers that, other rounds. But if use recursive ng-template without @defer angular renders it with help of only one changes detection round.
Unfortunately we can't prevent execution of template expressions previously created (by IntersectionObserver's changes detection rounds) embedded views because there is no way to control changes detection strategy per embedded view.
As I know, signals will be able to mark view for check and we will be able to avoid situation like above. Is that related to embedded view also or it is related only to component level view?

from angular.

Related Issues (20)

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.