Learn JavaScript

Lesson 5 of 9 · Error Handling: throw, try, catch, finally, and Reading a Stack Trace

Module 10 · Error Handling: throw, try, catch, finally, and Reading a Stack Trace

Errors Under the Hood: Completion Records, the Cost of try, and What Building an Error Really Costs

ProReading

Maria has heard two pieces of advice from older programmers. One: "never put try inside a hot loop, it kills performance". Two: "exceptions are expensive, avoid them". She wants to know if either is still true on Node 22. She also wants to know why finally can change what a function returns, which looked like magic in Lesson 02.

Checking what this lesson needs for you…

What is inside Errors Under the Hood: Completion Records, the Cost of try, and What Building an Error Really Costs

  1. Completion records: how the specification sees a block
  2. Why that rule matters
  3. The cost of a try block on the normal path
  4. The cost of building an Error
  5. What this means in practice
  6. Error.stackTraceLimit: how many frames
  7. Error.captureStackTrace: put a stack on anything, or hide a frame