Skip to content

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.

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" }),
);

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.