UNPKG

@botonic/plugin-contentful

Version:

## What Does This Plugin Do?

99 lines 2.87 kB
import { NodePerformanceMeasurer } from './measurer/node-performance-measurer'; import { WebPerformanceMeasurer } from './measurer/web-performance-measurer'; export const isBrowser = () => { // @ts-ignore return typeof IS_BROWSER !== 'undefined' ? // eslint-disable-next-line no-undef // @ts-ignore IS_BROWSER : typeof window !== 'undefined' && typeof window.document !== 'undefined' && !window.process; }; export class Profiler { /** * disables any profiling logic */ static isEnabled() { return Profiler.performanceMeasurer.isEnabled(); } /** * disables any profiling logic */ static disable() { Profiler.performanceMeasurer.disable(); } /** * enables profiling logic */ static enable() { Profiler.performanceMeasurer.enable(); } /** * get a pretty summary of your profiling */ static getSummaryAll() { return Profiler.performanceMeasurer.stringSummaryAll(); } /** * get a pretty summary 1 profiling */ static getSummary(name) { return Profiler.performanceMeasurer.stringSummary(name); } static getCallCount(name) { return Profiler.performanceMeasurer.callCount(name); } /** * clear all measurements */ static clear() { return Profiler.performanceMeasurer.clear(); } /** * annotation for a typescript class member * @constructor */ static Profile(customName) { return function (_target, _propertyKey, descriptor) { const f = descriptor.value; const newF = Profiler.profiledFunction(f, customName); // @ts-ignore descriptor.value = function (...args) { return newF.apply(this, args); }; return descriptor; }; } /** * creates a profiled function */ // eslint-disable-next-line @typescript-eslint/ban-types static profiledFunction(f, customName) { const name = customName || f.name; // @ts-ignore return function (...args) { const m = new Measure(name); // @ts-ignore const res = f.apply(this, args); m.end(); return res; }; } } Profiler.performanceMeasurer = isBrowser() ? new WebPerformanceMeasurer() : new NodePerformanceMeasurer(); export class Measure { constructor(name) { this.name = (name || '<NO_NAME>').substr(0, 40); this.uuid = this.name + String(Measure.lastUuid++); Profiler.performanceMeasurer.start(this.uuid); } end(passthrough) { Profiler.performanceMeasurer.end(this.uuid, this.name); return passthrough; } } Measure.lastUuid = 0; //# sourceMappingURL=profiler.js.map