Skip to content

[Bug]: "Map maximum size exceeded" in deepClone #16967

@darress

Description

@darress

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

babel.transformFromAstAsync(...) // Called from Angular compiler

Configuration file name

No response

Configuration

No response

Current and expected behavior

Current behavior: the deepClone (transformation\util\clone-deep.js) method throws error "Map maximum size exceeded" for very big ASTs, because the standard JS Map can only hold somewhat more than 16.000.000 keys, our AST has more objects than that.

Expected behavior: handling big ASTs without errors.

Environment

Babel: 7.24.7
Node: v18.20.4
NPM: 10.7.0
Windows 11

Possible solution

As a quick fix, I implemented a MultiMap class and replaced the standard Map with it in the deepClone implementation, it solved the problem:

class MultiMap {
    #maps = [new Map()];

    has(key) {
        return this.#maps.some(map => map.has(key));
    }

    get(key) {
        for(let map of this.#maps){
            let value = map.get(key);
            if (value)
                return value;
        }
        return undefined;
    }

    set(key, value) {
        if (this.#maps[this.#maps.length-1].size >= 16000000) {
            this.#maps.push(new Map());
        }
        this.#maps[this.#maps.length-1].set(key,value);
    }
}

And I replaced "new Map()" in the _default function with "new MultiMap()". After that the deepClone call threw no errors.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: core

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions