aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/bun/mmap/1.js11
-rw-r--r--examples/bun/mmap/2.js22
-rw-r--r--examples/bun/mmap/mmap.txt1
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