What is better global or local "require" ?

Lets say I need json in a few modules.

What is better:

a) have only one global  json = require “json” in main.lua

b ) have local json = require “json” in each module

Regards,

Damir.

Local’s are always better than globals except in rare situations where you have to write a lot of code to avoid it (but even in that case, you probably should consider refactoring your code to avoid that).

Does “require” in each module increases the app size ?

Or once you “require” a module, other “requires” use the first one ?

The module is included just once.  The “local” is really just causing it to be addressed in the memory space for the chunk it’s included in as opposed to the memory space where global’s are accessed.  It’s resource expensive to access global variables.

1 Like

Local’s are always better than globals except in rare situations where you have to write a lot of code to avoid it (but even in that case, you probably should consider refactoring your code to avoid that).

Does “require” in each module increases the app size ?

Or once you “require” a module, other “requires” use the first one ?

The module is included just once.  The “local” is really just causing it to be addressed in the memory space for the chunk it’s included in as opposed to the memory space where global’s are accessed.  It’s resource expensive to access global variables.