Organize Cloud Functions for max cold start performance and readability with TypeScript and Firebase

Doug Stevenson
Firebase Developers
7 min readMar 2, 2020

--

A while back I made a video about minimizing cold starts for functions deployed with the Firebase CLI using TypeScript:

It sums up the issue like this:

Suppose you have multiple functions exported from your index.ts that don’t all use the same static imports, like these two functions httpFn and firestoreFn:

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
admin.initializeApp()export const httpFn =
functions.https.onRequest(async (request, response) => {
// This function uses the Firebase Admin SDK
const snapshot = await admin.firestore()
.collection('users')
.doc('uid')
.get()
const data = snapshot.data()
response.send(data)
})
export const firestoreFn =
functions.firestore.document("users/{id}")
.onCreate(async (snapshot, context) => {
// This function does not use firebase-admin,
// and unnecessarily pays the cost of its import and init
console.log(snapshot.data())
return null
})

--

--