evanw / evanw/node-source-map-support

Excessive memory consumption when handling large bundles without source maps

Open Beginner friendly
#344 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
2.2k
Forks
223
PR merge metrics
No merged PRs in 30d

Description

**Environment:**

- Node.js with source-map-support
- several large JavaScript bundles (5–7 MB each)
- no source maps available for the bundles

**Problem:**

When an error is thrown in a large bundle, the source-map-support library unnecessarily retains the full file contents in memory. The file is loaded solely to check for a source map, but its contents are cached even after this one‑time operation. This results in excessive memory usage, especially problematic with multiple large bundles.

**Steps to reproduce:**

1. An error is thrown in a bundle (e.g., index.js)
2. `Error.prepareStackTrace()` is called
3. The call chain proceeds as follows: `wrapCallSite()` → `mapSourcePosition()` → `retrieveSourceMap("index.js")` → `retrieveSourceMapURL("index.js")` → `retrieveFile("index.js")`
4. The default `retrieveFile` handler loads the entire index.js file from disk and stores it in the `fileContentsCache` ([source](https://github.com/evanw/node-source-map-support/blob/master/source-map-support.js#L129))
5. `retrieveSourceMapURL()` fails to find a source map for the file
6. `mapSourcePosition()` stores an empty source map in its internal cache ([source](https://github.com/evanw/node-source-map-support/blob/master/source-map-support.js#L235))
7. The full contents of index.js remain in the `fileContentsCache` indefinitely, consuming memory

**Expected behavior:**

File contents should be used only once to check for the presence of a source map, and then either not cached at all or discarded immediately after the check. There is no need to retain large bundle files in memory after this operation.

**Actual behavior:**

Large bundle files (5–7 MB) are retained in the `fileContentsCache` after the first error, leading to unnecessary increase in memory footprint.

**Proposed solution:**

Avoid caching when no source map is found: skip storing file contents in `fileContentsCache` if the source map lookup fails.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in source-map-support.js at retrieveFile() and retrieveSourceMapURL(), then follow the mapSourcePosition() cache path described in the issue. Reproduce the lookup with a large bundle that has no source map and verify that its contents are not retained in fileContentsCache after the failed lookup.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
devtools
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.