I'm aware that I'm doing a computationally unfeasible thing, essentially doing Get all substrings of a string in JavaScript but with bytes of a 1MB exe instead of characters in a string.
But I wanted to see how many bytes all the segments would add up to, at least until my program crashed. Well it does crash, but I think my byte count is wrong.
const fs = require("fs");
const bytesPerKB = 1000;
const bytesPerMB = bytesPerKB * 1000;
const bytesPerGB = bytesPerMB * 1000;
function getAllSegments(buffer, skip = 1) {
let i, j, result = [], bytes = 0;
for (i = 0; i < buffer.length; i += skip) {
if (i % 1000 === 0) console.log('getting ranges for byte', i, 'with a total of', bytes / bytesPerGB, 'GB stored')
for (j = i + 1; j < buffer.length + 1; j++) {
const entry = buffer.slice(i, j)
bytes += entry.length
result.push(entry);
}
}
return result;
}
console.log('ready')
fs.promises.readFile('../data/scraped/test-1MB.exe').then(data => {
console.log('read file', data)
let segements = getAllSegments(data, 10000)
console.log('segments', segements);
})
output:
I'm pretty sure I don't have 8 TBs of storage on my PC, much less 8TB of swap space allocated. What'd I do wrong with the byte counting math?