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?

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.