Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Agents example
// agent-smith.js
* Receive shared array buffer in this worker.
onmessage = (message) => {
// Worker's view of the shared data.
let heapArray = new Int32Array(;
let indexToModify = 1;
heapArray[indexToModify] = 100;
// Send the index as a message back.
<!DOCTYPE html>
<title>Agents example</title>
NOTE: if you run this example locally, run it in Firefox,
since Chrome due to security reasons doesn't allow loading
web workers from a local file.
<script type="text/javascript">
// Shared data between this agent, and another worker.
let sharedHeap = new SharedArrayBuffer(16);
// Our view of the data.
let heapArray = new Int32Array(sharedHeap);
// Create a new agent (worker).
let agentSmith = new Worker('agent-smith.js');
agentSmith.onmessage = (message) => {
// Agent sends the index of the data it modified.
let modifiedIndex =;
// Check the data is modified:
console.log(heapArray[modifiedIndex]); // 100
// Send the shared data to the agent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment