Is there any perforamce implication of try catch block in node.js?

24 Views Asked by At

I am using Node.js and using error handling in functions itself. I will provide 2 sample code just want to know if there will be any performance implications in both these 2 methods.

public async testFunc(data: any) {

    let insertArray: IData[] = []

    data.forEach(async (item: any) => {
        const id_present = item[`${Constants.ID}`]
        const number_present = item[`${Constants.NUMBER}`]

        const newItem: IData = {
            id: item[`${Constants.ID}`],
            number: item[`${Constants.NUMBER}`],
            url: item[`${Constants.LINK}`]?.trim(),
            lob: item[`${Constants.LOB}`]?.trim(),
            status: (Constants.CODE),
            retry: 0,
            doc_id: curr_doc_id,
        }

        if (!id_present && !number_present) {
            newItem.status = Constants.FAILED
            newItem.failed_reason = Constants.MISSING
        }

        insertArray.push(newItem);
    });

    try {
        const saveResponse = DBModel.insertMany(insertArray);
        return Promise.resolve({ success: true });
    } catch (error) {
        return Promise.reject(error);
    }
}

OR

public async testFunc(data: any, res: Response) {
    try {
        // same work as above
        return Promise.resolve({ success: true });
    } catch (error) {
        return Promise.reject(error);
    }
}
1

There are 1 best solutions below

0
nikos fotiadis On

No, there are no performance implications for that. Both should be the same in terms of performance.