如何优化firebase函数的响应速度

can*_*wow 3 node.js firebase google-cloud-functions google-cloud-firestore

我使用基于 nodejs 6 和 firestore 的 firebase 函数,我这样的简单函数总是很慢。我还发现当我在函数中使用 set/add firestore 时,它​​总是慢可能 5-10 秒,这是 index.js,app 得到响应来自这个 queryUserDoc api。

`

const accountModel = require('./account');
exports.queryUserDoc = functions.https.onCall((data, context) => {
    const uid = context.auth.token.uid;
    return accountModel.getUserDocByUid(uid)
        .then(doc => {
            return JSON.stringify(({'errCode': ERROR_SUCCESS, 'data': doc.data()}));
        })
        .catch(err => {
            return JSON.stringify(({'errCode': err}));
        });
});
Run Code Online (Sandbox Code Playgroud)

` account.js 如下:

function getUserDocByUid(uid) {
    return db.collection(DB_COLLECTION_USER).doc(uid).get();
}
Run Code Online (Sandbox Code Playgroud)

当我的应用程序调用这个 api 时,我发现它很慢。控制台在下面 在此处输入图片说明

mar*_*rio 5

对我有用并显着提高了 firebase 函数速度的是更新我函数的位置。我位于欧洲,因此默认值最初设置为us-central1. 更新europe-west1速度后,速度从~5 秒变为~600 毫秒。如此处所述更改区域相对容易 https://firebase.google.com/docs/functions/locations 我刚刚按照他们的示例进行操作,我准备好了