Chapter 15: Performance and cost

Garbage collection

Also called GC, GC pause

The part of a runtime that finds memory nothing can reach any more and frees it, so you don't have to. It costs work, and some of that work can pause your program. Those pauses are one reason a fast function sometimes isn't.

Most of the time you can ignore it, and an agent should too. It matters when you see a tail with no obvious cause and code that creates a lot of short-lived objects in a hot loop. Check with a profile first: rewriting code to avoid allocation on a hunch makes it worse to read and may not help.

Say it to your agent

“Before you rewrite this loop to avoid allocating, run it with node --cpu-prof and tell me what share of the profile is (garbage collector). If it is under 5%, leave the code alone.”

The workshop

Name a word, or describe what you mean in everyday language.