diff options
author | Javier Lopez <graulopezjavier@gmail.com> | 2021-09-10 08:22:40 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-10 06:22:40 -0700 |
commit | 4b452d4efb8cbab48129189833ac8af9a1d02a2f (patch) | |
tree | 1d468a2f827e1fe7f86e0f3f0e6cd10bea2b5403 /runtime/lua/vim/shared.lua | |
parent | 3b3dbcf7b7ba5466e6ab643e256f2374b520a6b2 (diff) | |
download | rneovim-4b452d4efb8cbab48129189833ac8af9a1d02a2f.tar.gz rneovim-4b452d4efb8cbab48129189833ac8af9a1d02a2f.tar.bz2 rneovim-4b452d4efb8cbab48129189833ac8af9a1d02a2f.zip |
perf(lua): optimize vim.deep_equal #15236
By remembering the keys already compared in repeating a comparison is
avoided. Thanks: https://stackoverflow.com/a/32660766
Diffstat (limited to 'runtime/lua/vim/shared.lua')
-rw-r--r-- | runtime/lua/vim/shared.lua | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index 032b2b2cb5..18c1e21049 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -267,18 +267,23 @@ function vim.tbl_deep_extend(behavior, ...) end --- Deep compare values for equality +--- +--- Tables are compared recursively unless they both provide the `eq` methamethod. +--- All other types are compared using the equality `==` operator. +---@param a first value +---@param b second value +---@returns `true` if values are equals, else `false`. function vim.deep_equal(a, b) if a == b then return true end if type(a) ~= type(b) then return false end if type(a) == 'table' then - -- TODO improve this algorithm's performance. for k, v in pairs(a) do if not vim.deep_equal(v, b[k]) then return false end end - for k, v in pairs(b) do - if not vim.deep_equal(v, a[k]) then + for k, _ in pairs(b) do + if a[k] == nil then return false end end |