computed.bench.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { bench, describe } from 'vitest'
  2. import { type ComputedRef, type Ref, computed, effect, ref } from '../src'
  3. describe('computed', () => {
  4. bench('create computed', () => {
  5. computed(() => 100)
  6. })
  7. {
  8. const v = ref(100)
  9. computed(() => v.value * 2)
  10. let i = 0
  11. bench("write ref, don't read computed (without effect)", () => {
  12. v.value = i++
  13. })
  14. }
  15. {
  16. const v = ref(100)
  17. const c = computed(() => {
  18. return v.value * 2
  19. })
  20. effect(() => c.value)
  21. let i = 0
  22. bench("write ref, don't read computed (with effect)", () => {
  23. v.value = i++
  24. })
  25. }
  26. {
  27. const v = ref(100)
  28. const c = computed(() => {
  29. return v.value * 2
  30. })
  31. let i = 0
  32. bench('write ref, read computed (without effect)', () => {
  33. v.value = i++
  34. c.value
  35. })
  36. }
  37. {
  38. const v = ref(100)
  39. const c = computed(() => {
  40. return v.value * 2
  41. })
  42. effect(() => c.value)
  43. let i = 0
  44. bench('write ref, read computed (with effect)', () => {
  45. v.value = i++
  46. c.value
  47. })
  48. }
  49. {
  50. const v = ref(100)
  51. const computeds: ComputedRef<number>[] = []
  52. for (let i = 0, n = 1000; i < n; i++) {
  53. const c = computed(() => {
  54. return v.value * 2
  55. })
  56. computeds.push(c)
  57. }
  58. let i = 0
  59. bench("write ref, don't read 1000 computeds (without effect)", () => {
  60. v.value = i++
  61. })
  62. }
  63. {
  64. const v = ref(100)
  65. const computeds: ComputedRef<number>[] = []
  66. for (let i = 0, n = 1000; i < n; i++) {
  67. const c = computed(() => {
  68. return v.value * 2
  69. })
  70. effect(() => c.value)
  71. computeds.push(c)
  72. }
  73. let i = 0
  74. bench(
  75. "write ref, don't read 1000 computeds (with multiple effects)",
  76. () => {
  77. v.value = i++
  78. },
  79. )
  80. }
  81. {
  82. const v = ref(100)
  83. const computeds: ComputedRef<number>[] = []
  84. for (let i = 0, n = 1000; i < n; i++) {
  85. const c = computed(() => {
  86. return v.value * 2
  87. })
  88. computeds.push(c)
  89. }
  90. effect(() => {
  91. for (let i = 0; i < 1000; i++) {
  92. computeds[i].value
  93. }
  94. })
  95. let i = 0
  96. bench("write ref, don't read 1000 computeds (with single effect)", () => {
  97. v.value = i++
  98. })
  99. }
  100. {
  101. const v = ref(100)
  102. const computeds: ComputedRef<number>[] = []
  103. for (let i = 0, n = 1000; i < n; i++) {
  104. const c = computed(() => {
  105. return v.value * 2
  106. })
  107. computeds.push(c)
  108. }
  109. let i = 0
  110. bench('write ref, read 1000 computeds (no effect)', () => {
  111. v.value = i++
  112. computeds.forEach(c => c.value)
  113. })
  114. }
  115. {
  116. const v = ref(100)
  117. const computeds: ComputedRef<number>[] = []
  118. for (let i = 0, n = 1000; i < n; i++) {
  119. const c = computed(() => {
  120. return v.value * 2
  121. })
  122. effect(() => c.value)
  123. computeds.push(c)
  124. }
  125. let i = 0
  126. bench('write ref, read 1000 computeds (with multiple effects)', () => {
  127. v.value = i++
  128. computeds.forEach(c => c.value)
  129. })
  130. }
  131. {
  132. const v = ref(100)
  133. const computeds: ComputedRef<number>[] = []
  134. for (let i = 0, n = 1000; i < n; i++) {
  135. const c = computed(() => {
  136. return v.value * 2
  137. })
  138. effect(() => c.value)
  139. computeds.push(c)
  140. }
  141. effect(() => {
  142. for (let i = 0; i < 1000; i++) {
  143. computeds[i].value
  144. }
  145. })
  146. let i = 0
  147. bench('write ref, read 1000 computeds (with single effect)', () => {
  148. v.value = i++
  149. computeds.forEach(c => c.value)
  150. })
  151. }
  152. {
  153. const refs: Ref<number>[] = []
  154. for (let i = 0, n = 1000; i < n; i++) {
  155. refs.push(ref(i))
  156. }
  157. const c = computed(() => {
  158. let total = 0
  159. refs.forEach(ref => (total += ref.value))
  160. return total
  161. })
  162. let i = 0
  163. const n = refs.length
  164. bench('1000 refs, read 1 computed (without effect)', () => {
  165. refs[i++ % n].value++
  166. c.value
  167. })
  168. }
  169. {
  170. const refs: Ref<number>[] = []
  171. for (let i = 0, n = 1000; i < n; i++) {
  172. refs.push(ref(i))
  173. }
  174. const c = computed(() => {
  175. let total = 0
  176. refs.forEach(ref => (total += ref.value))
  177. return total
  178. })
  179. effect(() => c.value)
  180. let i = 0
  181. const n = refs.length
  182. bench('1000 refs, read 1 computed (with effect)', () => {
  183. refs[i++ % n].value++
  184. c.value
  185. })
  186. }
  187. })