UNPKG

fast-merge-async-iterators

Version:
45 lines 1.53 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const _1 = __importDefault(require(".")); async function* generator(name) { for (let i = 0;; i++) { yield name + i; await new Promise((resolve) => process.nextTick(resolve)); } } async function* ticker() { for (let i = 0;; i++) { yield "tick"; // Rarely ticking generator is evil, because it may cause reusing of the // same Promise in Promise.race() thousands of times which produces a // slowdown and memory leaks: https://github.com/nodejs/node/issues/17469 await new Promise((resolve) => setTimeout(resolve, 1000)); } } async function* caller() { yield* (0, _1.default)(generator("A"), generator("B"), generator("C")); } async function* callerWithTicker() { yield* (0, _1.default)(generator("A"), generator("B"), generator("C"), ticker()); } async function bench(func, max) { let count = 0; const t = Date.now(); for await (const msg of func()) { if (count >= max) { break; } count++; } const rate = Math.round((max / (Date.now() - t)) * 1000); console.log(`${func.name}: ${rate} yields/s; count=${count}`); } (async () => { const MAX = 500000; await bench(caller, MAX); await bench(callerWithTicker, MAX); })(); //# sourceMappingURL=example-bench.js.map