UNPKG

react-native-benchmark

Version:

React Native benchmarking library inspired by benchmark.js

74 lines (73 loc) 2.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Suite = void 0; const event_1 = require("./event"); const benchmark_1 = require("./benchmark"); const options_1 = require("./types/options"); class Suite { constructor() { this.benches = []; this.run = async (options = {}) => { const opts = { ...options_1.defaultOptions, ...options }; if (opts.async) { const suite = this; const array = []; const runBenchmark = function (b, opts) { return setTimeout(function () { b.run(opts); }); }; suite.benches.map(bench => { array.push(runBenchmark(bench, opts)); }); return await Promise.all(array); } else { return this.benches.map(bench => bench.run(opts)); } }; } add(name, fn) { this.benches.push(new benchmark_1.Benchmark(name, fn)); return this; } on(type, listener) { let counter = 0; const suite = this; switch (type) { case event_1.EventType.COMPLETE: // check when bench is complete this.benches.map(bench => bench.addListener(type, () => { if (++counter === this.benches.length) listener(new event_1.Event(type, suite)); // notify when all benches and suite are complete })); break; default: this.benches.map(bench => bench.addListener(type, listener)); } return this; } off(type, listener) { this.benches.map(bench => { if (listener) { bench.removeListener(type, listener); } else { bench.removeAllListeners(type); } }); return this; } toJSON() { let result = {}; this.benches.map(b => result[b.name] = b.stats.toJSON()); return result; } toString() { return this.benches.map(b => b.toString()).join('\n'); } } exports.Suite = Suite;