Skip to content

Why does node/io.js hang when printing to the console inside a loop? #1741

Closed
@pgkos

Description

@pgkos

Sorry if it is a dumb question/issue, but why does this code:

for (i = 0; i < 1000000; i++) {
  console.log('abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc' + i);
}

hang node for a few seconds (at around i = 1500) before resuming again? During the hang the amount of memory used by the node process increases very fast and the CPU usage is very high during that time.

On the other hand, if I write:

for (i = 0; i < 1000000; i++) {
  console.log(i);
}

then the node process does not hang at all.

The reason I am asking is that I have a piece of code which involves printing inside a loop, and it also hangs, but for much longer (like 30-60 seconds, before resuming again). The code prints the paths of all files in a directory (walking recursively): https://gist.github.com/pgkos/f0a650daf56aa49899e9.

The issue occurs on both io.js v2.0.2 and node.js v0.12.2, on Ubuntu 64-bit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.memoryIssues and PRs related to the memory management or memory footprint.processIssues and PRs related to the process subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions