33

From what I understand require is used to load large chunks of code (something like modules) although it can also load individual functions.

Autoload on the other side, only registers functions and defers the loading to execution time.

I've recently read an article which advocates the usage of autoload exclusively.

Is it better to autoload than to require? What are some typical use cases for each of these?

paprika
  • 1,944
  • 19
  • 26
caisah
  • 4,056
  • 1
  • 23
  • 43
  • 3
    @Gilles: FWIW, I disagree with your having removed tags `autoload` and `require` from this question. Adding tag `libraries` was fine, but now anyone searching, using tags, for questions about `autoload` or `require` will not find this one. Those are both important Emacs-Lisp functions that people will search for. Too bad. And think how helpful `elisp` and `libraries` (esp. `elisp`) will really be in practice for an Emacs site: they do not distinguish much in Emacsland, I'm afraid. (But no, I am not against having them and using them.) – Drew Sep 28 '14 at 02:11
  • @Drew I'm going by [the applicable meta discussion](http://meta.emacs.stackexchange.com/questions/32/should-i-use-function-name-or-behavior-for-tagging). If you think that this general principle doesn't apply here, please raise it on meta. – Gilles 'SO- stop being evil' Sep 28 '14 at 02:13
  • 1
    @Gilles: [Done](http://meta.emacs.stackexchange.com/a/58/105). Yes, I read that and figured that that was perhaps what had motivated you. I don't disagree with lots of general principles. That does not mean that following them in a blanket way is always TRT. – Drew Sep 28 '14 at 03:08
  • 3
    I agree with Drew: since this question is specifically about distinguishing the use cases for `autoload` and `require` I think those tags are justified. In fact, labeling this question [only] with `libraries` is over-generalizing. I for one use `require` to trigger loading of "my-foobar-cfg", which, as the name suggests, only contains just my configuration, *not* a library. – paprika Oct 06 '14 at 15:16
  • See also the manual, [When to Use Autoload](https://www.gnu.org/software/emacs/manual/html_node/elisp/When-to-Autoload.html). – Michael Allan Dec 28 '19 at 01:21

3 Answers3

29

autoload is not a substitute for require. Typically require is used to make sure that a certain file is loaded. autoload on the other hand gives Emacs a hint in which file to find a given function without loading the file right away. Only when the autoloaded function is called is the corresponding file loaded.

Basically with autoload you can delay the loading of a whole file to the moment when you actually need it. This is why (especially big) packages typically define their entry functions as autoloads.

If you want to do heavy customizations for a package usually you are out of luck with autoload. If you still want to avoid require then you can defer your customizations until after the file has been loaded by using eval-after-load.

The elisp manual covers this topic in the following sections:

Pooya
  • 113
  • 5
paprika
  • 1,944
  • 19
  • 26
  • 2
    Perhaps worth mentioning also: `autoload` is (more or less) about providing command definitions, so you can use those commands. `require` is about making *everything* in a library (and in libraries that library requires, recursively) available. There is more to Emacs than commands. (But yes, of course once an autoloaded command is invoked, its entire library is loaded.) – Drew Sep 27 '14 at 15:46
4

In terms of Emacs initialization, don't make either choice. GitHub user jwiegley has the excellent use-package declaration macro, that will require or autoload a package as determined by need. Best used to defer the loading of packages that can be loaded on-demand.

Andy
  • 398
  • 2
  • 6
3

If you want a certain feature to be loaded and available directly when your Emacs has started, use require, otherwise use autoload. I personally value startup time much (I actually restart Emacs a couple of times per week, sometimes several times per day), so I accept that it will take a little bit of time when Emacs autoloads a certain function when I need it. So, think about what you want to be available directly after Emacs is started and what can be loaded when needed.

Mathias Dahl
  • 554
  • 2
  • 7