• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const { Console } = require('console');
4const { Writable } = require('stream');
5const async_hooks = require('async_hooks');
6
7// Make sure that repeated calls to console.log(), and by extension
8// stream.write() for the underlying stream, allocate exactly 1 tick object.
9// At the time of writing, that is enough to ensure a flat memory profile
10// from repeated console.log() calls, rather than having callbacks pile up
11// over time, assuming that data can be written synchronously.
12// Refs: https://github.com/nodejs/node/issues/18013
13// Refs: https://github.com/nodejs/node/issues/18367
14
15const checkTickCreated = common.mustCall();
16
17async_hooks.createHook({
18  init(id, type, triggerId, resoure) {
19    if (type === 'TickObject') checkTickCreated();
20  }
21}).enable();
22
23const console = new Console(new Writable({
24  write: common.mustCall((chunk, encoding, cb) => {
25    cb();
26  }, 100)
27}));
28
29for (let i = 0; i < 100; i++)
30  console.log(i);
31