how to optimize firebase function's response s

2019-07-20 02:36发布

I use firebase function base on nodejs 6 and firestore,my simple function like this is always slow.I also found when i use set/add firestore in function,it always slow maybe 5-10 second this is index.js,app get response from this 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}));
        });
});

` account.js is below:

function getUserDocByUid(uid) {
    return db.collection(DB_COLLECTION_USER).doc(uid).get();
}

when my app call this api,i found it so slowly.console is below enter image description here

1条回答
霸刀☆藐视天下
2楼-- · 2019-07-20 03:03

What has worked for me and increased significantly the speed of firebase functions, was to update the location of my functions. I'm located in Europe so the default was initially set to us-central1 . After updating to europe-west1 speed went from ~5 seconds to ~600ms. It's relatively easy to change the region as described here https://firebase.google.com/docs/functions/locations I've just followed their example and i was ready to go

查看更多
登录 后发表回答