-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Description
Feature request
When a .json module is sufficiently large, webpack leverages JSON.parse to enhance performance.
webpack/lib/json/JsonGenerator.js
Lines 176 to 181 in 7168389
| // 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