App.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <script setup lang="ts">
  2. import { ref, shallowRef } from 'vue'
  3. import { buildData } from './data'
  4. import { defer, wrap } from './profiling'
  5. const selected = ref<number>()
  6. const rows = shallowRef<
  7. {
  8. id: number
  9. label: string
  10. }[]
  11. >([])
  12. function setRows(update = rows.value.slice()) {
  13. rows.value = update
  14. }
  15. const add = wrap('add', () => {
  16. rows.value = rows.value.concat(buildData(1000))
  17. })
  18. const remove = wrap('remove', (id: number) => {
  19. rows.value.splice(
  20. rows.value.findIndex(d => d.id === id),
  21. 1,
  22. )
  23. setRows()
  24. })
  25. const select = wrap('select', (id: number) => {
  26. selected.value = id
  27. })
  28. const run = wrap('run', () => {
  29. setRows(buildData())
  30. selected.value = undefined
  31. })
  32. const update = wrap('update', () => {
  33. const _rows = rows.value
  34. for (let i = 0; i < _rows.length; i += 10) {
  35. _rows[i].label += ' !!!'
  36. }
  37. setRows()
  38. })
  39. const runLots = wrap('runLots', () => {
  40. setRows(buildData(10000))
  41. selected.value = undefined
  42. })
  43. const clear = wrap('clear', () => {
  44. setRows([])
  45. selected.value = undefined
  46. })
  47. const swapRows = wrap('swap', () => {
  48. const _rows = rows.value
  49. if (_rows.length > 998) {
  50. const d1 = _rows[1]
  51. const d998 = _rows[998]
  52. _rows[1] = d998
  53. _rows[998] = d1
  54. setRows()
  55. }
  56. })
  57. async function bench() {
  58. for (let i = 0; i < 30; i++) {
  59. rows.value = []
  60. await defer()
  61. await runLots()
  62. }
  63. }
  64. </script>
  65. <template>
  66. <h1>Vue.js Vapor Benchmark</h1>
  67. <div
  68. id="control"
  69. style="display: flex; flex-direction: column; width: fit-content; gap: 6px"
  70. >
  71. <button @click="bench">Benchmark mounting</button>
  72. <button id="run" @click="run">Create 1,000 rows</button>
  73. <button id="runlots" @click="runLots">Create 10,000 rows</button>
  74. <button id="add" @click="add">Append 1,000 rows</button>
  75. <button id="update" @click="update">Update every 10th row</button>
  76. <button id="clear" @click="clear">Clear</button>
  77. <button id="swaprows" @click="swapRows">Swap Rows</button>
  78. </div>
  79. <div id="time"></div>
  80. <table>
  81. <tbody>
  82. <tr
  83. v-for="row of rows"
  84. :key="row.id"
  85. :class="{ danger: row.id === selected }"
  86. >
  87. <td>{{ row.id }}</td>
  88. <td>
  89. <a @click="select(row.id)">{{ row.label }}</a>
  90. </td>
  91. <td>
  92. <button @click="remove(row.id)">x</button>
  93. </td>
  94. <td class="col-md-6"></td>
  95. </tr>
  96. </tbody>
  97. </table>
  98. </template>
  99. <style>
  100. .danger {
  101. background-color: red;
  102. }
  103. </style>