forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
10 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
const fs = require('fs'); | ||
|
||
/** | ||
* Writes `lines` to a file in batches to limit memory usage. | ||
* Writes `lines` to a file in chunks to limit memory usage. | ||
* | ||
* @param {string[]} lines - each string in the array is written on its own line | ||
* @param {string} filePath | ||
* @param {number} batchSize | ||
* @param {number} chunkSize | ||
*/ | ||
function writeToFileInBatches(lines, filePath, batchSize) { | ||
function writeToFileInChunks(lines, filePath, chunkSize) { | ||
// If the file already exists, clear it by writing empty string. | ||
if (fs.existsSync(filePath)) { | ||
fs.writeFileSync(filePath, "", "utf-8"); | ||
} | ||
|
||
// Append batches to the file (will be created if it doesn't exist). | ||
for (let i = 0; i < lines.length; i += batchSize) { | ||
let batchedLines = lines.slice(i, i + batchSize).join("\n") + "\n"; | ||
fs.appendFileSync(filePath, batchedLines, "utf-8"); | ||
// Append chunks to the file (will be created if it doesn't exist). | ||
for (let i = 0; i < lines.length; i += chunkSize) { | ||
let linesToWrite = lines.slice(i, i + chunkSize).join("\n") + "\n"; | ||
fs.appendFileSync(filePath, linesToWrite, "utf-8"); | ||
} | ||
} | ||
|
||
module.exports = { | ||
writeToFileInBatches | ||
writeToFileInChunks | ||
} |