apiWatch.bench.ts 959 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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(
  36. 'update ref to trigger watchEffect (scheduled but not executed)',
  37. () => {
  38. v.value = i++
  39. },
  40. )
  41. }
  42. {
  43. const v = ref(100)
  44. watchEffect(() => {
  45. v.value
  46. })
  47. let i = 0
  48. bench('update ref to trigger watchEffect (executed)', async () => {
  49. v.value = i++
  50. await nextTick()
  51. })
  52. }