Repository: louislam/uptime-kuma
File: server/model/monitor.js (JavaScript)

Code before:
    }

    /**
     * prepare preloaded data for efficient access
     * @param {Array} monitorData IDs & active field of monitor to get
     * @returns {Promise<LooseObject<any>>} object
     */
    static async preparePreloadData(monitorData) {
        const monitorIDs = monitorData.map(monitor => monitor.id);
        const notifications = await Monitor.getMonitorNotification(monitorIDs);
        const tags = await Monitor.getMonitorTag(monitorIDs);
        const maintenanceStatuses = await Promise.all(
            monitorData.map(monitor => Monitor.isUnderMaintenance(monitor.id))
        );
        const childrenIDs = await Promise.all(
            monitorData.map(monitor => Monitor.getAllChildrenIDs(monitor.id))
        );
        const activeStatuses = await Promise.all(
            monitorData.map(monitor => Monitor.isActive(monitor.id, monitor.active))
        );
        const forceInactiveStatuses = await Promise.all(
            monitorData.map(monitor => Monitor.isParentActive(monitor.id))
        );

        // Organize preloaded data for efficient access
        return {
            notifications: notifications.reduce((acc, row) => {
                acc[row.monitor_id] = acc[row.monitor_id] || {};
                acc[row.monitor_id][row.notification_id] = true;
                return acc;
            }, {}),
            tags: tags.reduce((acc, row) => {
                acc[row.monitor_id] = acc[row.monitor_id] || [];
                acc[row.monitor_id].push({ name: row.name,
                    color: row.color
                });
                return acc;
            }, {}),
            maintenanceStatus: Object.fromEntries(monitorData.map((m, index) => [ m.id, maintenanceStatuses[index] ])),
            childrenIDs: Object.fromEntries(monitorData.map((m, index) => [ m.id, childrenIDs[index] ])),
            activeStatus: Object.fromEntries(monitorData.map((m, index) => [ m.id, activeStatuses[index] ])),
            forceInactive: Object.fromEntries(monitorData.map((m, index) => [ m.id, !forceInactiveStatuses[index] ])),
        };

    }

    /**
     * Gets Parent of the monitor
     * @param {number} monitorID ID of monitor to get
     * @returns {Promise<LooseObject<any>>} Parent
     */

Review comment (bug):
the `.reduce` calls with object-changes here are really hard to parse for me and thus likely other devs.
If there were a bug in here, I would not be able to tell.

Please move this into regular for-loops.
While moving this, please also check if [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#:~:text=A%20Map%20does%20not%20contain,but%20this%20is%20seldom%20done.) is not more appropriate.
I found [this blog post](https://www.builder.io/blog/maps) with some claims that this might increase performance (if that code happends to be a factor we are spending a part of the remaining `700ms`..)

Code after:
        ]);
    }

    /**
     * Gets monitor tags of multiple monitor
     * @param {Array} monitorIDs IDs of monitor to get
     * @returns {Promise<LooseObject<any>>} object
     */
    static async getMonitorTag(monitorIDs) {
        return await R.getAll(`
            SELECT monitor_tag.monitor_id, tag.name, tag.color
            FROM monitor_tag
            JOIN tag ON monitor_tag.tag_id = tag.id
            WHERE monitor_tag.monitor_id IN (?)
        `, [
            monitorIDs,
        ]);
    }

    /**
     * prepare preloaded data for efficient access
     * @param {Array} monitorData IDs & active field of monitor to get
     * @returns {Promise<LooseObject<any>>} object
     */
    static async preparePreloadData(monitorData) {

        const notificationsMap = new Map();
        const tagsMap = new Map();
        const maintenanceStatusMap = new Map();
        const childrenIDsMap = new Map();
        const activeStatusMap = new Map();
        const forceInactiveMap = new Map();
        const pathsMap = new Map();

        if (monitorData.length > 0) {
            const monitorIDs = monitorData.map(monitor => monitor.id);
            const notifications = await Monitor.getMonitorNotification(monitorIDs);
            const tags = await Monitor.getMonitorTag(monitorIDs);
            const maintenanceStatuses = await Promise.all(monitorData.map(monitor => Monitor.isUnderMaintenance(monitor.id)));
            const childrenIDs = await Promise.all(monitorData.map(monitor => Monitor.getAllChildrenIDs(monitor.id)));
            const activeStatuses = await Promise.all(monitorData.map(monitor => Monitor.isActive(monitor.id, monitor.active)));
            const forceInactiveStatuses = await Promise.all(monitorData.map(monitor => Monitor.isParentActive(monitor.id)));
            const paths = await Promise.all(monitorData.map(monitor => Monitor.getAllPath(monitor.id, monitor.name)));

            notifications.forEach(row => {
                if (!notificationsMap.has(row.monitor_id)) {
                    notificationsMap.set(row.monitor_id, {});
                }
                notificationsMap.get(row.monitor_id)[row.notification_id] = true;
            });