UNPKG

timeandreturn

Version:

Get the elapsed time of a block of code and return the result of that block of code.

36 lines 1.25 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const performance_now_1 = __importDefault(require("performance-now")); const format_1 = require("./format"); /** * Call callback with the time taken to run block, and return the result of block * * If block returns a Promise, call callback after promise resolves. * * Elapsed time is given in milliseconds. */ function timeAndReturn(callback, block) { const startTime = performance_now_1.default(); const returnValue = block(); if (returnValue instanceof Promise) { returnValue.then(() => { callback(performance_now_1.default() - startTime); }); } else { callback(performance_now_1.default() - startTime); } return returnValue; } exports.timeAndReturn = timeAndReturn; /** * Log the time taken to run block, and return the result of block */ function logTimeAndReturn(block) { return timeAndReturn((elapsedTime) => console.log(format_1.formatBlockTime(block, elapsedTime)), block); } exports.logTimeAndReturn = logTimeAndReturn; //# sourceMappingURL=index.js.map