Skip to content

Mutations on arrays - why reconstruct array in memory every time? #2511

@garygreen

Description

@garygreen

When adding an item to an array (e.g. a todo list, adding a new Todo) it's advertised to do this (using the spread operator):

  const [tasks, setTasks] = createSignal([])

  const addTask = function() {
    setTasks([...tasks(), { name: 'new task' }]);
  }

The problem with this is that it feels very wasteful - it's throwing away existing memory and then re-creating the array from scratch.

I'm new to solidjs so maybe it's my misunderstanding (I'm exploring switching from Vue, which doesn't re-create arrays you can simply push onto it, due to it being a Proxy).

I see there is a reconcile feature, but under the hood it looks like it's using https://github.com/solidjs/solid/blob/3d3207dd3aeb84c2a38377cf9f3b895995c2d969/packages/solid/store/src/modifiers.ts#L10 which re-creates the whole array in memory each time.

This isn't much of an issue with small arrays, but when dealing with super large arrays it's so wasteful to keep reconstructing that memory, garbage collection etc just to push one item onto it.

Is there a consideration for optimizing that in solidjs? Thank you for assistance! 😀

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