我試圖使用 Node.js 創建一個云函式。起初我為每個不同的功能創建了 3 個檔案,但實際上它花費了將近 30-45 秒來完成所有這些檔案,僅用于身份驗證和檢查設備。然后,我決定將所有檔案組合成一個函式,現在它有 185 個復雜度。它真的變得更快了,只需要 10-14 秒。
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const level = require("../../library/level");
const hamsu = require("../../library/useful");
const db = admin.firestore();
const medium = level.medium;
exports.Authentication = functions.runWith(medium).https.onCall(async (data, context) => {
if (context.app == undefined) {
throw new functions.https.HttpsError(
"failed-precondition",
"The function must be called from an App Check verified app.");
}
if (!context.auth) {
throw new functions.https.HttpsError("failed-precondition", "Unauthorized User is trying to access the App.");
}
let code;
const token = data.token;
});
那么,我想問的是,復雜度高達 185 真的會導致服務器超載嗎?感謝您提供任何提示和技巧。對不起,我是新手。
uj5u.com熱心網友回復:
185 對于認知復雜性來說非常高,在以下情況下是不可取的:
- 其他人需要理解和維護你的代碼
- 您可能會對該功能的作業方式記憶淡化
- 該函式中有/需要在其他地方復制的代碼
一般而言,最佳實踐是保持低認知復雜度的代碼,盡可能少到沒有代碼重復實體。
順便說一句:我希望你喜歡重構程序!一些 IDE 將提供內置的重構工具,可以在一定程度上自動化該程序。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443524.html
標籤:javascript 节点.js 扑 谷歌云功能
