I am working on developing an API for a test suite. One of the methods in the API requires the use of a library that isn’t needed anywhere else in the API.
My question is whether the require statement for using the library should be placed inside the method or every time the API loads. The library isn’t very large so it won’t have a significant effect on performance.
If the dependency has good namespace organization (won’t pollute the global namespace) and isn’t large (won’t slow startup times), I’d say put it at the top of the file. It’s where people expect to find require statements. If it has either of those problems, consider putting it in the most limited scope possible.