Background
I am building an API using AWS Lambda and API Gateway. Rather than splitting each API endpoint into individual lambda functions I am wrapping them into a single library and using the aws-serverless-express
library.
Question
Given that only a portion of the entire API might be used in a single Lambda execution— from a memory utilization standpoint (to cut down on cost) is there a difference between:
var myModule = require("mymodule");
...
function handleSomething1()
{
myModule.doSomething();
}
function handleSomething2()
{
...
}
or
function handleSomething()
{
require("mymodule").doSomething();
}
function handleSomething2()
{
...
}
So for example, a single API request might result in only handleSomething2
being called before the Lambda function is powered down. In that case are we effectively wasting memory by calling var myModule = require("mymodule");
up top?
I suppose the more direct question is, when I var myModule = require("mymodule")
does the node.js runtime actually allocate memory for myModule
at that moment? Or is it effectively a no-op until I actually do something with myModule
?