Skip to content

Performance of mutating large collections: arrays vs objects #649

@keriwarr

Description

@keriwarr

🙋‍♂ Question

Hello!

When I first read the immer performance docs, it didn't occur to me at the time that choice of Array vs Object for storing data might make a large impact on performance.

I use immer to manage a redux store in which records are keyed by their ID inside of an object. This performs perfectly well when the number of records is in the low 1000s, however we are looking to push our application to the level of supporting tens or possibly even hundreds of thousands of records. What I've found is that for sufficiently large collections it can take vastly longer to update a single record if the collection is an object as opposed to an array.

  1. Is there any known workaround such that I can get array-like performance, but continue to use an object? (Or some other data structure that gives us O(1) retrieval by ID?)
  2. If there is no such workaround, would you accept a PR to the docs clarifying this?

Link to repro

https://codesandbox.io/s/immer-sandbox-g64y4

In this demo, I create an array collection and an object collection of todos (just like in the test:perf code) but each with size 200,000, and then measure the time it takes to update a single record within the collection.

image

Update: Maps seem to be about an order of magnitude faster than objects but still a few times slower than arrays (I tried out a few different collection sizes). However, I don't think that I would want to take the hit of using an unserializable data structure in redux.

https://codesandbox.io/s/immer-sandbox-09zyb

image

Environment

We only accept questions against the latest Immer version.

  • Immer version: 7.0.7
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)

P.S. thanks for this awesome package <3

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