Skip to content

Reactivity: optimize full array iterations #673

@jods4

Description

@jods4

What problem does this feature solve?

I propose a performance optimization.

Applications use plenty of arrays. Full array iteration is extremely common:

  • v-for performs a for (i = 0; i < length; i ) array[i] loop when rendering;
  • computed or watch often call .filter(), .sort(), .map() and co. All of these result in a full iteration;
  • user code can also do a for loop or call .forEach().

This top use-case is tracked like any normal object, key by key. If I have a reactive array containing 100 items, then calling .filter in a computed will result in 102 new tracking entries: one for filter, one for length and 100 more from [0] to [99].
Each one results in a Set allocation one entry in the reverse deps array the required upkeep every time the effect runs again.

Because both (1) full array iteration is very common; and (2) arrays can be quite big, making tracking expensive; I think this may be worthy of a special optimization.

Here's one idea: detect full enumeration and only register a single symbol ALL_KEY in response.
It could work like this:

  • Add arrayRanges: Map<Array, number> on effect.
  • When track is called on an array target, if the key is numeric:
    • if target is not in activeEffect.arrayRanges and key is 0, add it with value 0.
    • if target is in arrayRanges and key === arrayRanges.get(target) 1 then increment arrayRanges.
  • At the end of run:
    • for each arrayRanges that is equals to target.length - 1, create a dependency with symbol ALL_KEY.
    • Clear arrayRanges
  • When calling trigger, if target is an array and key is numeric, additionally trigger ALL_KEY.

What does the proposed API look like?

No new public API

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions