Skip to content

Optimize findViewByNodeId with an O(1) view map (m.__router_viewMap) #99

Description

@iObject

Optimize findViewByNodeId with an O(1) view map (m.__router_viewMap)

Problem

findViewByNodeId previously iterated over all children of viewTarget and keepAliveViewTarget to find a view by node ID. This is O(n) in the stack depth and is called on every goBack and popToCheckpoint.

Proposal

Introduce m.__router_viewMap — an associative array keyed by node ID that tracks every live view and whether it currently lives in viewTarget or keepAliveViewTarget.

findViewByNodeId now does a single AA lookup instead of looping over node children.

Map maintenance

The map is kept in sync at every point where a view changes ID, location, or lifetime:

Event Action
View added to viewTarget in addViewToStack viewMap[id] = { ..., fromKeepAlive: false }
View suspended to keepAliveViewTarget (forward nav or suspendView) viewMap[id] = { ..., fromKeepAlive: true }
keepAlive view ID reassigned on reuse in addViewToStack viewMap.delete(old id) before reassign
View reparented back to viewTarget (_goBack, _popToCheckpoint) viewMap[id] = { ..., fromKeepAlive: false }
View destroyed in closeView viewMap.delete(id)
View destroyed inline in addViewToStack (non-keepAlive close loop) viewMap.delete(id)
Router destroyed in _destroy m.__router_viewMap = {}

Impact

  • findViewByNodeId is now O(1) regardless of stack depth.
  • No change to observable behaviour — the map mirrors the existing node-tree structure exactly.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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