I'm trying to reduce function execution time with caching. To do this, I'm lazy importing a module into global scope when needed, and then reusing it.
I tried to follow this guide: https://firebase.google.com/docs/functions/tips#do_lazy_initialization_of_global_variables
and this blog post: https://medium.com/firebase-developers/organize-cloud-functions-for-max-cold-start-performance-and-readability-with-typescript-and-9261ee8450f0
From my understanding, the global vars should be cached, but if I leave the function overnight, the first run triggers an import, which is slow. Subsequent calls reuse the import.
I would expect that as long as the function is "alive" due to the "minInstance" param, the instance should reuse the init.
Here's the code:
let BigImport;
export const dostuff = functionsV2_onCall(
{
memory: '2GiB',
minInstances: 1,
maxInstances: 50,
},
async request => {
console.time('timing - IMPORT');
if (!BigImport) {
console.log('actually importing!');
BigImport = await import('./BigImport');
} else {
console.log('not importing yaaaay!');
}
console.timeEnd('timing - IMPORT');
const result = await BigImport.default.foo(request);
return result;
},
);
Here's the function details in GCP:

minInstances isn't guaranteed to leave the same one instance running all the time. Cloud Functions is free to shut down and restart instances when it sees fit. As such, you can't eliminate cold start times - all you can do is minimize the number of times they happen.
From the documentation (emphasis mine):
All of the bolded words here suggest that there are no guarantees whatsoever that an instance stays up for as long as you might expect. You can only minimize cold starts - you can't eliminate them.
If you're not willing to accept any cold start behavior, then you will need to use a normal provisioned server and accept the monetary and maintenance costs that come with it.