Back to all reviewers

defer expensive operations

neovim/neovim
Based on 2 comments
Txt

Avoid performing expensive operations (like module loading, event emissions, or resource allocation) until they are actually needed. This pattern significantly improves startup time and runtime performance by reducing unnecessary work.

Performance Optimization Txt

Reviewer Prompt

Avoid performing expensive operations (like module loading, event emissions, or resource allocation) until they are actually needed. This pattern significantly improves startup time and runtime performance by reducing unnecessary work.

Key strategies:

  • Move require calls from initialization code into the actual function implementations
  • Remove or consolidate unnecessary events/operations that create overhead
  • Defer resource-intensive setup until first use

Example of lazy require pattern:

-- Instead of eager loading at plugin initialization:
local foo = require("foo")
vim.api.nvim_create_user_command("MyCommand", function()
    foo.do_something()
end, {})

-- Defer the require until command execution:
vim.api.nvim_create_user_command("MyCommand", function()
    local foo = require("foo")  -- Load only when needed
    foo.do_something()
end, {})

This approach ensures that modules and their dependencies are only loaded when the functionality is actually used, rather than eagerly loading everything at startup. The same principle applies to other expensive operations like network calls, file I/O, or complex computations.

2
Comments Analyzed
Txt
Primary Language
Performance Optimization
Category

Source Discussions