As you may have seen below; here are two different approaches to run different cloud functions as foo.js and bar.js.
In Method #1, admin, database and messaging modules initialized in index.js and passed as parameter to each related functions. Moreover, in Method #2, those parameters are defined inside each functions.
Which approach is applicable to minimize cold start time during running each function?
Method #1
index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const database = admin.database();
const messaging = admin.messaging();
const fooFunction = require('./foo');
exports.fooFunction = functions.database.ref('/users/messages_inbox').onCreate( (snapshot, context) => { fooFunction.handler(database, messaging, snapshot, context) });
const barFunction = require('./bar');
exports.barFunction = functions.database.ref('/users').onCreate( (snapshot, context) => { barFunction.handler(database, snapshot, context) });
foo.js
exports.handler = (database, messaging, snapshot, context) => {
// some function
}
bar.js
exports.handler = (database, snapshot, context) => {
// some function
}
Method #2
index.js
const functions = require('firebase-functions');
const fooFunction = require('./foo');
exports.fooFunction = functions.database.ref('/users/messages_inbox').onCreate( fooFunction.handler );
const barFunction = require('./bar');
exports.barFunction = functions.database.ref('/users').onCreate( barFunction.handler );
foo.js
exports.handler = (snapshot, context) => {
const admin = require('firebase-admin');
const database = admin.database();
const messaging = admin.messaging();
// some function
}
bar.js
exports.handler = (snapshot, context) => {
const admin = require('firebase-admin');
const database = admin.database();
// some function
}