Sourcemaps for stacktraces for typescript are slow

Posted by Alex D'Andrea on 9 November 2023

I recently discovered a problem with AWS Lambda scripts suddently erroring out randomly without giving a clear error message. I was finally able to track it down, but it took some time.

I was using serverless framework to deploy an application to AWS Lambda, and while the application is not new, I try to keep up with the latest defaults from the framework. So a time ago I found this setting was given by default in serverless typescript projects:

environment:
  NODE_OPTIONS: "--enable-source-maps --stack-trace-limit=1000"

This would enable sourcemaps and increase the stack trace limit from 10 (the default) to 1000. Sourcemaps are used converts stack locations from the generated Javascript locations to the file locations in the original typescript files, thus helping developers to see where the error comes from in the original source.

Caveat is that mapping stacktraces is really slow - just as documented in nodejs, as I then found out:

Note, enabling source maps can introduce latency to your application when Error.stack is accessed. If you access Error.stack frequently in your application, take into account the performance implications of –enable-source-maps.

For my application this performance implication manifested in a ~2.5 sec delay when a stacktrace was written into a logfile while accessing the .stack field.

Conclusion

As documented there is a performance impact - so huge it is not worth it: I had to turn the flag off. I would have not expected such a massive performance hit by just that one sentence.