Only execute expensive operations when they are actually necessary. Avoid paying performance costs for functionality that may not be used or when conditions don’t require the expensive computation.
Key strategies:
Examples:
-- Bad: Always pay the fs_stat cost
local stat = vim.uv.fs_stat(f)
-- Good: Only pay the cost when needed
if follow_param then
local stat = vim.uv.fs_stat(f)
end
-- Bad: Eager loading causes performance hit
complete = vim.treesitter.language._complete,
-- Good: Lazy loading avoids eager initialization
complete = function(...) return vim.treesitter.language._complete(...) end,
-- Bad: Everyone pays the cost regardless of usage
vim.api.nvim_create_autocmd('DiagnosticChanged', {
callback = function() -- expensive string formatting end
})
-- Good: Only setup when default statusline is used
if using_default_statusline then
vim.api.nvim_create_autocmd('DiagnosticChanged', {
callback = function() -- expensive string formatting end
})
end
This approach prevents unnecessary performance overhead by ensuring expensive operations are only executed when their results will actually be used or when the conditions genuinely require them.