Is there a cost to calling a computed signal in a loop?

Discussão em 'Angular' iniciado por Stephen Paul, Março 25, 2025.

  1. Stephen Paul

    Stephen Paul Guest

    I just did a quick performance test as follows:

    const someExpensiveComputation = computed(() => ...)

    const before = performance.now();

    for (let i = 0; i < 10000; i++)
    someExpensiveComputation();

    console.log(`Time taken: ${performance.now() - before}`)


    This performs very well. The above code logs 5 (as in 5 milliseconds). As we know, Angular is caching previous results and not recomputing if none of the computed signals dependencies change.

    Is calling a computed signal within a loop considered a code-smell? Bad practice? To be avoided out of principle?

    Continue reading...

Compartilhe esta Página