Question presented to candidate:
"A teammate wrote {{ totalPrice() }} where totalPrice is a class method that loops over the cart. Another wrote the same thing as a computed() signal. Both render the same number. What is the actual difference, and does it matter in Angular 22?"
What a strong answer should cover:
- A method or getter in a template is called on every check of that view, whether or not its inputs changed; it caches nothing.
- A
computed()is memoised: it re-runs only when a signal it read changed, and returns the cached value otherwise. - With OnPush (the default in v22) a view is only checked when marked dirty, so method calls are bounded by how often the view is checked; with Eager (the old Default) they run on every pass that reaches it.
computedalso cuts off propagation: if its result is unchanged, downstream computeds do not re-run.- A method that only reads signals is still correct, just not cached; the concern is cost and side effects, not correctness.
- Alternatives for non-signal data: a pure pipe.
Clarifying questions expected:
- "Is the data in signals already, or plain fields or observables?"
Code / implementation expected: Yes β count how often each runs across change detection cycles.