fileReadJSONBuffers
Prefer reading JSON files as buffers when using JSON.parse for better performance.
✅ This rule is included in the node stylistic preset.
JSON.parse() can parse buffers directly without needing to convert them to strings first.
Reading files as buffers when parsing JSON avoids unnecessary string conversion overhead.
When reading a JSON file with fs.readFile() or fs.readFileSync() and then parsing it with JSON.parse(), there’s no need to specify UTF-8 encoding.
The file can be read as a buffer, and JSON.parse() will handle the buffer directly.
Examples
Section titled “Examples”const packageJson = JSON.parse(await fs.readFile("./package.json", "utf8"));const data = JSON.parse(await fs.readFile("./data.json", "utf-8"));const config = JSON.parse(fs.readFileSync("./config.json", "utf8"));const settings = JSON.parse( fs.readFileSync("./settings.json", { encoding: "utf-8" }),);const packageJson = JSON.parse(await fs.readFile("./package.json"));const data = JSON.parse(await fs.readFile("./data.json"));const config = JSON.parse(fs.readFileSync("./config.json"));const settings = JSON.parse(fs.readFileSync("./settings.json"));Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you explicitly prefer to always describe encoding parameters, this rule might not be for you. Additionally, if you choose to manually choose which nuanced parsing behavior you prefer, you may disagree with this rule and choose not to enable it.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
unicorn/prefer-json-parse-buffer