2
0

apiWatch.bench.ts 942 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { nextTick, ref, watch, watchEffect } from '../src'
  2. import { bench } from 'vitest'
  3. bench('create watcher', () => {
  4. const v = ref(100)
  5. watch(v, v => {})
  6. })
  7. {
  8. const v = ref(100)
  9. watch(v, v => {})
  10. let i = 0
  11. bench('update ref to trigger watcher (scheduled but not executed)', () => {
  12. v.value = i++
  13. })
  14. }
  15. {
  16. const v = ref(100)
  17. watch(v, v => {})
  18. let i = 0
  19. bench('update ref to trigger watcher (executed)', async () => {
  20. v.value = i++
  21. return nextTick()
  22. })
  23. }
  24. {
  25. bench('create watchEffect', () => {
  26. watchEffect(() => {})
  27. })
  28. }
  29. {
  30. const v = ref(100)
  31. watchEffect(() => {
  32. v.value
  33. })
  34. let i = 0
  35. bench('update ref to trigger watchEffect (scheduled but not executed)', () => {
  36. v.value = i++
  37. })
  38. }
  39. {
  40. const v = ref(100)
  41. watchEffect(() => {
  42. v.value
  43. })
  44. let i = 0
  45. bench('update ref to trigger watchEffect (executed)', async () => {
  46. v.value = i++
  47. await nextTick()
  48. })
  49. }