deepcopy_spec.lua 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. local N = 20
  2. local function tcall(f, ...)
  3. local ts = vim.uv.hrtime()
  4. for _ = 1, N do
  5. f(...)
  6. end
  7. return ((vim.uv.hrtime() - ts) / 1000000) / N
  8. end
  9. local function build_shared(n)
  10. local t = {}
  11. local a = {}
  12. local b = {}
  13. local c = {}
  14. for _ = 1, n do
  15. t[#t + 1] = {}
  16. local tl = t[#t]
  17. for _ = 1, n do
  18. tl[#tl + 1] = a
  19. tl[#tl + 1] = b
  20. tl[#tl + 1] = c
  21. end
  22. end
  23. return t
  24. end
  25. local function build_unique(n)
  26. local t = {}
  27. for _ = 1, n do
  28. t[#t + 1] = {}
  29. local tl = t[#t]
  30. for _ = 1, n do
  31. tl[#tl + 1] = {}
  32. end
  33. end
  34. return t
  35. end
  36. describe('vim.deepcopy()', function()
  37. local function run(name, n, noref)
  38. it(string.format('%s entries=%d noref=%s', name, n, noref), function()
  39. local t = name == 'shared' and build_shared(n) or build_unique(n)
  40. local d = tcall(vim.deepcopy, t, noref)
  41. print(string.format('%.2f ms', d))
  42. end)
  43. end
  44. run('unique', 50, false)
  45. run('unique', 50, true)
  46. run('unique', 2000, false)
  47. run('unique', 2000, true)
  48. run('shared', 50, false)
  49. run('shared', 50, true)
  50. run('shared', 2000, false)
  51. run('shared', 2000, true)
  52. end)