Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Feature request: API for memoizing unchanged collection subtrees in .map, .filter, etc #1750

Copy link
Copy link
Open
@jedwards1211

Description

@jedwards1211
Issue body actions

If you approve of this I'd be willing to work on a PR!

Since it's extremely handy to use a React/Redux-style top down approach with lots of collection.map(item => <SomeElement item={item} />) operations during rendering, but this architecture is really not scalable, it would be nice to make .map, .filter and other methods a bit more scalable.

For context: I'm coding up a node-red style visual programming interface and thinking about the fact as the list of nodes and wires grows, UI speed when dragging a single node around will degrade below 30fps as the number of nodes/wires gets longer and longer...

Basically some kind of API could cache the previous result for any given subtree and avoid redoing any work on subtrees that haven't changed.

This would bring the cost of each rerender down to (I assume) O(log(n)) if just a single entry in the collection has changed.

How the API could potentially look

const memoized = MemoizedCollection(c => c.filter(isEven).map(double))
const prevInput = Map([...])
const prevOutput = memoized(prevInput)

const nextInput = prev.set('foo', 3)
const nextOutput = memoized(nextInput)

It would be awesome with a custom React hook

export function useMemoizedCollection(input, ops, dependencies = []) {
  const memoized = React.useMemo(() => MemoizedCollection(ops), dependencies)
  return memoized(input)
}

export const List = ({items}) => {
  const itemElements = useMemoizedCollection(
    items,
    c => c.filter(someFilter).map((value, key) => <li key={key}>{value}</li>),
    []
  )
  return (
    <ul>
      {[...items.values()]}
    </ul>
  )
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.