UNPKG

perf-marks

Version:

The simplest and lightweight solution for User Timing API in Javascript.

101 lines (100 loc) 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var is_user_timing_api_supported_1 = require("./is-user-timing-api-supported"); // Map() is not used in order to decrease the bundle var marksMap = {}; /** * Get the current time based on User Timing API or Date * * @returns number * */ var getTimeNow = function () { if (is_user_timing_api_supported_1.isUserTimingAPISupported) { return performance.now(); } return Date.now ? Date.now() : +new Date(); }; /** * Clear marks and measure of performance event * * @param markName - Performance marker to be checked * * @returns void * */ var clear = function (markName) { marksMap[markName] = undefined; if (!is_user_timing_api_supported_1.isUserTimingAPISupported) { return; } performance.clearMeasures(markName); performance.clearMarks(markName); }; exports.clear = clear; /** * Start performance measure of event * * @param markName - Performance marker to be started * * @returns number * */ var start = function (markName) { if (is_user_timing_api_supported_1.isUserTimingAPISupported) { performance.mark(markName); } marksMap[markName] = getTimeNow(); }; exports.start = start; /** * Finishes performance measure of event and * clear marks and measure if applicable * * @param markName - Performance marker to be checked * @param markNameToCompare - Optional mark to compare to * * @returns PerfMarksPerformanceEntry * */ var end = function (markName, markNameToCompare) { try { var startTime = marksMap[markName]; if (!is_user_timing_api_supported_1.isUserTimingAPISupported) { return startTime ? { duration: getTimeNow() - startTime, startTime: startTime } : {}; } performance.measure(markName, markName, markNameToCompare || undefined); var entry = performance.getEntriesByName(markName).pop(); if (entry) { return entry; } return {}; } catch (e) { // If previous mark was missing for some reason, this will throw. // This could only happen if something in event loop crashed // in an unexpected place earlier. // Don't pile on with more errors. return {}; } finally { // Clear marks immediately to avoid growing buffer. clear(markName); } }; exports.end = end; /** * Clear all marks and measures of performance event * * @returns void * */ var clearAll = function () { marksMap = {}; if (!is_user_timing_api_supported_1.isUserTimingAPISupported) { return; } performance.clearMeasures(); performance.clearMarks(); }; exports.clearAll = clearAll;