diff options
author | 2022-03-27 16:21:59 -0700 | |
---|---|---|
committer | 2022-03-27 16:21:59 -0700 | |
commit | 4bf5dc1df9f4eb1c913d9c9e40fcf3f718f5ebad (patch) | |
tree | 12019ea0cf9283c18911467b8245e24ddeab4e1c | |
parent | b6a3a655ae6ab1febfb773ed7593e6f7331cd514 (diff) | |
download | bun-4bf5dc1df9f4eb1c913d9c9e40fcf3f718f5ebad.tar.gz bun-4bf5dc1df9f4eb1c913d9c9e40fcf3f718f5ebad.tar.zst bun-4bf5dc1df9f4eb1c913d9c9e40fcf3f718f5ebad.zip |
Add Bun.mmap example
-rw-r--r-- | examples/bun/mmap/1.js | 11 | ||||
-rw-r--r-- | examples/bun/mmap/2.js | 22 | ||||
-rw-r--r-- | examples/bun/mmap/mmap.txt | 1 |
3 files changed, 34 insertions, 0 deletions
diff --git a/examples/bun/mmap/1.js b/examples/bun/mmap/1.js new file mode 100644 index 000000000..1c4f2c969 --- /dev/null +++ b/examples/bun/mmap/1.js @@ -0,0 +1,11 @@ +const map = Bun.mmap("./mmap.txt", { shared: true }); +const utf8deocder = new TextDecoder("utf-8"); + +let old = new TextEncoder().encode("12345"); + +setInterval(() => { + old = old.sort((a, b) => (Math.random() > 0.5 ? -1 : 1)); + console.log(`changing mmap to ~> ${utf8deocder.decode(old)}`); + + map.set(old); +}, 4); diff --git a/examples/bun/mmap/2.js b/examples/bun/mmap/2.js new file mode 100644 index 000000000..c4b68bd9a --- /dev/null +++ b/examples/bun/mmap/2.js @@ -0,0 +1,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)}`); + } +} diff --git a/examples/bun/mmap/mmap.txt b/examples/bun/mmap/mmap.txt new file mode 100644 index 000000000..6931040dd --- /dev/null +++ b/examples/bun/mmap/mmap.txt @@ -0,0 +1 @@ +43521
\ No newline at end of file |