refactor(lua): more efficient vim.tbl_islist

No need to run a full iteration of the table. Simply return false when
the next key isn't what we expect.
This commit is contained in:
Lewis Russell
2024-03-06 09:40:11 +00:00
committed by Lewis Russell
parent ea44f74d84
commit 3d2aeec68d

View File

@@ -652,19 +652,22 @@ function vim.tbl_islist(t)
return false return false
end end
local num_elem = vim.tbl_count(t) if next(t) == nil then
if num_elem == 0 then
return getmetatable(t) ~= vim._empty_dict_mt return getmetatable(t) ~= vim._empty_dict_mt
else end
for i = 1, num_elem do
if t[i] == nil then local j = 1
for _ in
pairs(t--[[@as table<any,any>]])
do
if t[j] == nil then
return false return false
end end
j = j + 1
end end
return true return true
end end
end
--- Counts the number of non-nil values in table `t`. --- Counts the number of non-nil values in table `t`.
--- ---