/js-multi-threading-promise-api

POC javascript multi-threading promise api

Primary LanguageJavaScript

Multi-threading in the browser with promise API

  1. create a worker script that imports all the computationally intensive functions you want to run in their own threads (see thread.js).
  2. setup worker to listen for payloads with function name, function args, and uuid. With such a payload, the worker can invoke the function (b/c it is already in scope from step 1) with the passed arguments. It then creates a broadcast channel with the uuid and sends the result of the computation.
  3. in the main event-loop thread we create a promise that sends function name, args, and uuid to a worker and resolves when a channel identified by the uuid gets a message (see spawn.js).

Now all we need to do is initialize a thread pool and call our spawn function (see benchmark.js).s

Usage

// in worker scope
// function expensive(num) { ... }

const threadPool = createThreadPool('thread.js')
Promise.all(nums.map(num => spawn(threadPool.worker, 'expensive', num)))

Run Benchmark

  • start local server e.g. python3 -m http.server
  • go to http://localhost:8000/ (or whichever port you set)
  • open dev console
  • see console log
STARTING WITHOUT WORKERS
FINISHED AFTER 5.2 s

STARTING WITH WORKERS

GET http://localhost:8000/thread.js
    [HTTP/1.0 200 OK 2ms]
GET http://localhost:8000/thread.js
    [HTTP/1.0 200 OK 2ms]
GET http://localhost:8000/thread.js
    [HTTP/1.0 200 OK 2ms]
GET http://localhost:8000/thread.js
    [HTTP/1.0 200 OK 2ms]
GET http://localhost:8000/thread.js
    [HTTP/1.0 200 OK 2ms]
GET http://localhost:8000/thread.js
    [HTTP/1.0 200 OK 2ms]

FINISHED AFTER 2.028 s

Notes