Skip to content

Commit aa9f39b

Browse files
committed
avoid copying array
1 parent d767423 commit aa9f39b

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

frameworks/keyed/valtio/src/main.jsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const N = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "s
1212
const random = (max) => Math.round(Math.random() * 1000) % max;
1313

1414
let nextId = 1;
15-
function buildData(count) {
16-
const data = new Array(count);
17-
for (let i = 0; i < count; i++) {
15+
function assignData(data, offset = 0) {
16+
const count = data.length;
17+
for (let i = offset; i < count; i++) {
1818
data[i] = {
1919
id: nextId++,
2020
label: `${A[random(A.length)]} ${C[random(C.length)]} ${N[random(N.length)]}`,
@@ -27,15 +27,19 @@ const state = proxy({ data: [], selected: 0 });
2727
const dispatch = (action) => {
2828
switch (action.type) {
2929
case "RUN":
30-
state.data = buildData(1000);
30+
state.data = new Array(1000);
31+
assignData(state.data);
3132
state.selected = 0;
3233
break;
3334
case "RUN_LOTS":
34-
state.data = buildData(10000);
35+
state.data = new Array(10000);
36+
assignData(state.data);
3537
state.selected = 0;
3638
break;
3739
case "ADD":
38-
state.data = state.data.concat(buildData(1000));
40+
const offset = state.data.length;
41+
state.data.length += 1000;
42+
assignData(state.data, offset);
3943
state.selected = 0;
4044
break;
4145
case "UPDATE": {

0 commit comments

Comments
 (0)