0

In this official Firebase example it's described how to structure cloud functions to minimize cold start times.

The main idea seems to be the following in the index.ts file:

export const httpFn =
functions.https.onRequest(async (request, response) => {
    await (await import('./fn/httpFn')).default(request, response)
})

In other words using await( await import()). This example is however in typescript. How would this look in node.js? I've found some examples. Eg. this one.

In there the index.js file has:

exports.helloWorld = require('./hello-world').helloWorld

And the individual function files look like:

const functions = require('firebase-functions')

exports.helloWorld = functions.https.onRequest((request, response) => {
    const output = {
        di: 'is cool',
    }
    response.send(output)
})

Is this the optimal method to minimize cold start time in node.js? Given I can't use "await import". Will this structure only load the necessary dependencies for each individual function? Or will it still load all dependencies across all required (imported) files?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

1 Answers1

1

That blog post you're referring to is not an "official sample". It's a description of my own personal thoughts and opinions.

What you're showing here is not equivalent to what's shown in the blog post. If you want a pure JavaScript implementation of TypeScript's async import, you will have to write code to mimic that. require() is not asynchronous and doesn't achieve the same thing.

I suggest reading other questions on Stack Overflow on this topic:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441