Skip to content

Commit 757f1da

Browse files
committed
Merge branch 'LittleSound-perf-update-vue-and-vue-vapor-code'
2 parents 8f03a97 + a4894c6 commit 757f1da

File tree

4 files changed

+62
-42
lines changed

4 files changed

+62
-42
lines changed

frameworks/keyed/vue-vapor/src/App.vue

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
11
<script setup vapor>
2-
import { ref, shallowRef } from "vue";
2+
import { ref, shallowRef, triggerRef, watch } from "vue";
33
import { buildData } from "./data";
44
55
const selected = ref();
66
const rows = shallowRef([]);
77
8-
function setRows(update = rows.value.slice()) {
9-
rows.value = update;
10-
}
11-
128
function add() {
13-
rows.value = rows.value.concat(buildData(1000));
9+
rows.value.push(...buildData(1000));
10+
triggerRef(rows);
1411
}
1512
1613
function remove(id) {
1714
rows.value.splice(
1815
rows.value.findIndex((d) => d.id === id),
1916
1
2017
);
21-
setRows();
18+
triggerRef(rows);
2219
}
2320
2421
function select(id) {
2522
selected.value = id;
2623
}
2724
2825
function run() {
29-
setRows(buildData());
26+
rows.value = buildData();
3027
selected.value = undefined;
3128
}
3229
3330
function update() {
3431
const _rows = rows.value;
35-
for (let i = 0; i < _rows.length; i += 10) {
36-
_rows[i].label += " !!!";
32+
for (let i = 0, len = _rows.length; i < len; i += 10) {
33+
_rows[i].label.value += " !!!";
3734
}
38-
setRows();
3935
}
4036
4137
function runLots() {
42-
setRows(buildData(10000));
38+
rows.value = buildData(10000);
4339
selected.value = undefined;
4440
}
4541
4642
function clear() {
47-
setRows([]);
43+
rows.value = [];
4844
selected.value = undefined;
4945
}
5046
@@ -55,9 +51,20 @@ function swapRows() {
5551
const d998 = _rows[998];
5652
_rows[1] = d998;
5753
_rows[998] = d1;
58-
setRows();
54+
triggerRef(rows)
5955
}
6056
}
57+
58+
// Reduce the complexity of `selected` from O(n) to O(1).
59+
function createSelector(source) {
60+
const cache = {}
61+
watch(source, (val, old) => {
62+
if (old != undefined) cache[old].value = false
63+
if (val != undefined) cache[val].value = true
64+
})
65+
return id => (cache[id] ??= shallowRef(false)).value
66+
}
67+
const isSelected = createSelector(selected)
6168
</script>
6269
6370
<template>
@@ -96,10 +103,10 @@ function swapRows() {
96103
</div>
97104
<table class="table table-hover table-striped test-data">
98105
<tbody>
99-
<tr v-for="row of rows" :key="row.id" :class="{ danger: row.id === selected }" :data-label="row.label">
106+
<tr v-for="row of rows" :key="row.id" :class="{ danger: isSelected(row.id) }" :data-label="row.label">
100107
<td class="col-md-1">{{ row.id }}</td>
101108
<td class="col-md-4">
102-
<a @click="select(row.id)">{{ row.label }}</a>
109+
<a @click="select(row.id)">{{ row.label.value }}</a>
103110
</td>
104111
<td class="col-md-1">
105112
<a @click="remove(row.id)">

frameworks/keyed/vue-vapor/src/data.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { shallowRef } from "vue"
2+
13
let ID = 1
24

35
function _random(max) {
@@ -64,12 +66,13 @@ export function buildData(count = 1000) {
6466
for (let i = 0; i < count; i++)
6567
data.push({
6668
id: ID++,
67-
label:
69+
label: shallowRef(
6870
adjectives[_random(adjectives.length)] +
69-
' ' +
70-
colours[_random(colours.length)] +
71-
' ' +
72-
nouns[_random(nouns.length)]
71+
' ' +
72+
colours[_random(colours.length)] +
73+
' ' +
74+
nouns[_random(nouns.length)],
75+
),
7376
})
7477
return data
7578
}

frameworks/non-keyed/vue-vapor/src/App.vue

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
11
<script setup vapor>
2-
import { ref, shallowRef } from "vue";
2+
import { ref, shallowRef, triggerRef, watch } from "vue";
33
import { buildData } from "./data";
44
55
const selected = ref();
66
const rows = shallowRef([]);
77
8-
function setRows(update = rows.value.slice()) {
9-
rows.value = update;
10-
}
11-
128
function add() {
13-
rows.value = rows.value.concat(buildData(1000));
9+
rows.value.push(...buildData(1000));
10+
triggerRef(rows);
1411
}
1512
1613
function remove(id) {
1714
rows.value.splice(
1815
rows.value.findIndex((d) => d.id === id),
1916
1
2017
);
21-
setRows();
18+
triggerRef(rows);
2219
}
2320
2421
function select(id) {
2522
selected.value = id;
2623
}
2724
2825
function run() {
29-
setRows(buildData());
26+
rows.value = buildData();
3027
selected.value = undefined;
3128
}
3229
3330
function update() {
3431
const _rows = rows.value;
35-
for (let i = 0; i < _rows.length; i += 10) {
36-
_rows[i].label += " !!!";
32+
for (let i = 0, len = _rows.length; i < len; i += 10) {
33+
_rows[i].label.value += " !!!";
3734
}
38-
setRows();
3935
}
4036
4137
function runLots() {
42-
setRows(buildData(10000));
38+
rows.value = buildData(10000);
4339
selected.value = undefined;
4440
}
4541
4642
function clear() {
47-
setRows([]);
43+
rows.value = [];
4844
selected.value = undefined;
4945
}
5046
@@ -55,9 +51,20 @@ function swapRows() {
5551
const d998 = _rows[998];
5652
_rows[1] = d998;
5753
_rows[998] = d1;
58-
setRows();
54+
triggerRef(rows)
5955
}
6056
}
57+
58+
// Reduce the complexity of `selected` from O(n) to O(1).
59+
function createSelector(source) {
60+
const cache = {}
61+
watch(source, (val, old) => {
62+
if (old != undefined) cache[old].value = false
63+
if (val != undefined) cache[val].value = true
64+
})
65+
return id => (cache[id] ??= shallowRef(false)).value
66+
}
67+
const isSelected = createSelector(selected)
6168
</script>
6269
6370
<template>
@@ -96,10 +103,10 @@ function swapRows() {
96103
</div>
97104
<table class="table table-hover table-striped test-data">
98105
<tbody>
99-
<tr v-for="row of rows" :class="{ danger: row.id === selected }" :data-label="row.label">
106+
<tr v-for="row of rows" :class="{ danger: isSelected(row.id) }" :data-label="row.label">
100107
<td class="col-md-1">{{ row.id }}</td>
101108
<td class="col-md-4">
102-
<a @click="select(row.id)">{{ row.label }}</a>
109+
<a @click="select(row.id)">{{ row.label.value }}</a>
103110
</td>
104111
<td class="col-md-1">
105112
<a @click="remove(row.id)">

frameworks/non-keyed/vue-vapor/src/data.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { shallowRef } from "vue"
2+
13
let ID = 1
24

35
function _random(max) {
@@ -64,12 +66,13 @@ export function buildData(count = 1000) {
6466
for (let i = 0; i < count; i++)
6567
data.push({
6668
id: ID++,
67-
label:
69+
label: shallowRef(
6870
adjectives[_random(adjectives.length)] +
69-
' ' +
70-
colours[_random(colours.length)] +
71-
' ' +
72-
nouns[_random(nouns.length)]
71+
' ' +
72+
colours[_random(colours.length)] +
73+
' ' +
74+
nouns[_random(nouns.length)],
75+
),
7376
})
7477
return data
7578
}

0 commit comments

Comments
 (0)