diff --git a/lib/CachedInputFileSystem.js b/lib/CachedInputFileSystem.js index 022e72c0..40e3f459 100644 --- a/lib/CachedInputFileSystem.js +++ b/lib/CachedInputFileSystem.js @@ -368,6 +368,19 @@ class CacheBackend { */ _storeResult(path, err, result) { if (this._data.has(path)) return; + + if (err) { + // Clone error object to be stored in cache to allow for the original + // error, and its internal representation to be picked up by GC. Thus + // reducing the memory footprint of caching such results in memory. + // Also, omitting expensive calculation of the stacktrace as it gets + // ignored most of the time anyway + const lightErrorProps = Object.getOwnPropertyDescriptors(err); + delete lightErrorProps.stack; + + err = Object.create(Object.getPrototypeOf(err), lightErrorProps); + } + const level = this._levels[this._currentLevel]; this._data.set(path, { err, result, level }); level.add(path);