diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000000000..bb15b4fa1dba8a --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-04-30 - Refactored fs.readFileSync calls +**Learning:** Calling `.toString()` on the Buffer returned by `fs.readFileSync(path)` causes unnecessary intermediate memory allocations and string conversion overhead. Passing the encoding directly as `fs.readFileSync(path, 'utf8')` is faster and more memory efficient. +**Action:** Always prefer `fs.readFileSync(path, 'utf8')` over chaining `.toString()` in Node.js scripts. diff --git a/packages/@expo/cli/src/start/project/__tests__/devices-test.ts b/packages/@expo/cli/src/start/project/__tests__/devices-test.ts index 47cca6966ff94d..3eb4feb23905b1 100644 --- a/packages/@expo/cli/src/start/project/__tests__/devices-test.ts +++ b/packages/@expo/cli/src/start/project/__tests__/devices-test.ts @@ -26,7 +26,7 @@ describe('devices info', () => { const file = path.join(projectRoot, '.expo', 'devices.json'); expect(fs.existsSync(file)).toBe(true); - const { devices } = JSON.parse(fs.readFileSync(file, 'utf8').toString()); + const { devices } = JSON.parse(fs.readFileSync(file, 'utf8')); expect(devices.length).toBe(1); expect(devices[0].installationId).toBe('test-device-id'); }); diff --git a/packages/@expo/cli/src/utils/mergeGitIgnorePaths.ts b/packages/@expo/cli/src/utils/mergeGitIgnorePaths.ts index dab487df678413..0f3aa7844c47b9 100644 --- a/packages/@expo/cli/src/utils/mergeGitIgnorePaths.ts +++ b/packages/@expo/cli/src/utils/mergeGitIgnorePaths.ts @@ -35,8 +35,9 @@ export function mergeGitIgnorePaths( return null; } - const targetGitIgnore = fs.readFileSync(targetGitIgnorePath).toString(); - const sourceGitIgnore = fs.readFileSync(sourceGitIgnorePath).toString(); + // Read using utf8 directly to avoid buffer allocation + const targetGitIgnore = fs.readFileSync(targetGitIgnorePath, 'utf8'); + const sourceGitIgnore = fs.readFileSync(sourceGitIgnorePath, 'utf8'); const merged = mergeGitIgnoreContents(targetGitIgnore, sourceGitIgnore); // Only rewrite the file if it was modified. if (merged.contents) { diff --git a/packages/@expo/config-plugins/src/android/Package.ts b/packages/@expo/config-plugins/src/android/Package.ts index 96222ebf4ba553..984c8a1f301840 100644 --- a/packages/@expo/config-plugins/src/android/Package.ts +++ b/packages/@expo/config-plugins/src/android/Package.ts @@ -125,7 +125,8 @@ export async function renameJniOnDiskForType({ filesToUpdate.forEach((filepath: string) => { try { if (fs.lstatSync(filepath).isFile() && ['.h', '.cpp'].includes(path.extname(filepath))) { - let contents = fs.readFileSync(filepath).toString(); + // Read using utf8 directly to avoid buffer allocation + let contents = fs.readFileSync(filepath, 'utf8'); contents = contents.replace( new RegExp(transformJavaClassDescriptor(currentPackageName).replace(/\//g, '\\/'), 'g'), transformJavaClassDescriptor(packageName) @@ -207,7 +208,8 @@ export async function renamePackageOnDiskForType({ filesToUpdate.forEach((filepath: string) => { try { if (fs.lstatSync(filepath).isFile()) { - let contents = fs.readFileSync(filepath).toString(); + // Read using utf8 directly to avoid buffer allocation + let contents = fs.readFileSync(filepath, 'utf8'); if (path.extname(filepath) === '.kt') { contents = replacePackageName(contents, currentPackageName, kotlinSanitizedPackageName); } else {