Skip to content

$state.snapshot does not preserve holes in a sparse array #14653

@Refzlund

Description

@Refzlund

Describe the bug

When attempting to create an array with empty slots/holes, the empty slots become "undefined" rather than holes, when using $state.snapshot(state):

<script>
    let arr = []

    arr[5] = true
    console.log('native', arr) // native (6) [empty x 5, true]

    let state = $state([])
    state[5] = true
    console.log('state', $state.snapshot(state)) // state (6) [undefined, undefined, undefined, undefined, undefined, true]
</script>

<div>
    <code>2 in arr</code>: {2 in arr} <!-- false -->
    <br>
    <code>5 in arr</code>: {5 in arr} <!-- true -->
</div>
<div>
    <code>2 in $state.snapshot(state)</code>: {2 in $state.snapshot(state)} <!-- true (expected false) -->
    <br>
    <code>5 in $state.snapshot(state)</code>: {5 in $state.snapshot(state)} <!-- true -->
</div>
<div>
    <code>2 in state</code>: {2 in state} <!-- false -->
    <br>
    <code>5 in state</code>: {5 in state} <!-- true -->
</div>

Reproduction

https://svelte.dev/playground/08ac2b6b28754224a77b9924f7d1a483?version=5.10.0

Logs

Console was cleared
running Svelte compiler version 5.10.0
native (6) [empty × 5, true]
state (6) [undefined, undefined, undefined, undefined, undefined, true]

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (12) x64 Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
    Memory: 17.48 GB / 31.93 GB
  Binaries:
    Node: 20.17.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD
    bun: 1.1.37 - ~\.bun\bin\bun.EXE
  Browsers: {}

### Browser
Vivaldi	7.0.3495.23 (Stable channel) (64-bit) 
Revision	72d89227db29f524d89b1af38163f46e4ed7746e
OS	Windows 11 Version 23H2 (Build 22631.4460)
JavaScript	V8 13.0.245.20
User Agent	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36

Severity

annoyance

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions