Search code examples
luagloballocallove2d

Is it faster to use local `foo = require "foo"` rather than `foo = require "foo"`


Will my program have faster access to the functions/variables stored in foo, if foo is local? And if i use local foo = require "foo" in multiple files will foo.lua be loaded multiple times?


Solution

  • Yes. Access to local values do one less hash lookup and will be faster. If it will be noticeable or not depends on how you use it - i.e. if amount of access to this table is relatively high to anything else your program does.

    require will only load any given module once. As docs say:

    The function starts by looking into the package.loaded table to determine whether modname is already loaded. If it is, then require returns the value stored at package.loaded[modname].