0

I'm creating a neovim plugin written in Lua, and I have to react to the BufAdd and BufUnload events with a function call. I have 3 options:

  1. write the function in Lua (like the rest of the plugin) and call it via:
autocmd BufAdd,BufUnload * lua require('myplugin').myfunction(args)
  1. write the function in Vimscript and place it in the autoload folder:
autocmd BufAdd,BufUnload * call myplugin#myfunction(args)
  1. write the function in Vimscript and place it in the plugin folder:
autocmd BufAdd,BufUnload * call MyPluginMyFunction(args)

I'd like to know which one is the best from a speed/efficiency standpoint. The function is question is around ~10 lines of non computationally intensive code.

3
  • 1
    "Autoload" (just like "require") = load-on-demand. It's nothing about speed. 10 lines is a negligible amount even if you have 1000 open/close operations. Especially as they are all triggered by a (dreadfully slow) organic creature. If there's no other reason then simply putting all code into a file where autocommands could be even more preferable.
    – Matt
    May 8 at 11:47
  • 2
    If you want to have your function available for Vim and not only neovim, I would put it into an autoloaded folder. But that makes only sense, if there are cases when the function does not need to be loaded on startup. I don't know how fast the lua functions are, but those hurt my eyes. May 8 at 12:10
  • 1
    Define faster and more efficient. Faster at what? To execute? To load? Etc.
    – D. Ben Knoble
    May 8 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.