export async function measure<T>(label: string, callback: () => Promise<T>): Promise<T> {
    const start = performance.now();

    try {
        const result = await callback();

        const duration = performance.now() - start;

        if (process.env.NODE_ENV === "development") {
            console.log(`%c[PERF] ${label} ${duration.toFixed(0)} ms`, `color:${getPerfColor(duration)}`);
        }

        return result;
    } catch (error) {
        const duration = performance.now() - start;

        console.error(`[PERF ERROR] ${label}`, `${duration.toFixed(2)} ms`);

        throw error;
    }
}

function getPerfColor(ms: number) {
    if (ms < 100) return "green";

    if (ms < 300) return "orange";

    return "red";
}