Skip to content

Add a JSON generator option to avoid JSON.parse #19319

@colinaaa

Description

@colinaaa

Feature request

When a .json module is sufficiently large, webpack leverages JSON.parse to enhance performance.

// Use JSON because JSON.parse() is much faster than JavaScript evaluation
const jsonStr = /** @type {string} */ (stringifySafe(finalJson));
const jsonExpr =
jsonStr.length > 20 && typeof finalJson === "object"
? `/*#__PURE__*/JSON.parse('${jsonStr.replace(/[\\']/g, "\\$&")}')`
: jsonStr.replace(/"__proto__":/g, '["__proto__"]:');

But JSON.parse is not faster in all cases (see the motivation below).

What is the expected behavior?

A new option to allow generate plain object literal instead of JSON.parse for .json modules.

What is motivation or use case for adding/changing the behavior?

In Lynx, we are using compiled bytecodes on the main-thread. So using object literal is faster and smaller than JSON.parse.

How should this be implemented in your opinion?

It would be great if an option like module.generator.json.avoidJSONParse could be added to avoid generating JSON.parse

Are you willing to work on this yourself?
yes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions