Created
February 16, 2026 11:49
-
-
Save rg443a/040a9a6e8d6909575f82d7a26c876244 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const fs = require('fs'); | |
| const buf = Buffer.allocUnsafe(400 * 1024 * 1024); | |
| const regex = /your-pattern-here/g; | |
| let leftover = Buffer.alloc(0); | |
| while (true) { | |
| const bytesRead = fs.readSync(0, buf, 0, buf.length); | |
| if (bytesRead === 0) break; | |
| const current = Buffer.concat([leftover, buf.subarray(0, bytesRead)]); | |
| let end = (bytesRead === buf.length) ? current.lastIndexOf('\n') + 1 : current.length; | |
| if (end <= 0) end = current.length; | |
| const str = current.toString('utf8', 0, end); | |
| let match; | |
| while ((match = regex.exec(str)) !== null) console.log(match[0]); | |
| leftover = Buffer.from(current.subarray(end)); | |
| } |
This file contains hidden or 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
| const fs = require('fs'); | |
| const fd = fs.openSync('large_file.txt', 'r'); | |
| const size = fs.fstatSync(fd).size; | |
| const buf = Buffer.allocUnsafe(400 * 1024 * 1024); | |
| const regex = /your-pattern-here/g; | |
| for (let offset = 0; offset < size; ) { | |
| const bytesRead = fs.readSync(fd, buf, 0, buf.length, offset); | |
| let end = (offset + bytesRead < size) ? buf.lastIndexOf('\n', bytesRead) + 1 : bytesRead; | |
| if (end <= 0) end = bytesRead; | |
| const str = buf.toString('utf8', 0, end); | |
| let match; | |
| while ((match = regex.exec(str)) !== null) { | |
| console.log(match[0]); | |
| } | |
| offset += end; | |
| } | |
| fs.closeSync(fd); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment