aboutsummaryrefslogtreecommitdiff
path: root/examples/mmap/2.js
blob: c4b68bd9a9e05b4f2d126d1c50067d149269fd2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const map = Bun.mmap("./mmap.txt");

function buffer_hash(buffer) {
  let hash = 0;
  for (let i = 0; i < buffer.length; i++) {
    hash = (hash << 5) - hash + buffer[i];
    hash |= 0; // Convert to 32bit integer
  }
  return hash;
}

const decoder = new TextDecoder();

let hash = buffer_hash(map);
console.log(decoder.decode(map));

while (true) {
  if (buffer_hash(map) !== hash) {
    hash = buffer_hash(map);
    console.log(`mmap changed to ~> ${decoder.decode(map)}`);
  }
}