When is the stack of Error objects generated?

Viewed 66

Every time I instantiate an Error object there is a .stack field. Assuming that building a stacktrace is a costly operation, my natural reaction is to reserve their use for exceptional cases.

However I remember reading that in v8 the stacktrace is only calculated upon read of the .stack field.

Can I assume that new Error(...) is just as expensive as new Object(...) if I never read the stack ?

Is this a feature of the JS engine or a standard behavior?

1 Answers
Related