Skip to content

Commit 57c1571

Browse files
committed
v0.8.2
1 parent c54dca8 commit 57c1571

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2969
-122
lines changed

CHANGE.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v0.8.2
2+
3+
- Bun is supported via the Node.js package.
4+
- Tests cases are added for Bun.
5+
- Benchmark project is added.
6+
17
## v0.8.0
28

39
- Supporting language interop is dropped. The @bitair/linker.js package should be used instead.

README.md

+69-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Concurrent.js is a library that enables non-blocking computation on JavaScript R
99
- [x] Sharing workers
1010
- [x] Parallel execution
1111
- [x] Reactive concurrency
12-
- [ ] Sandboxing
12+
- [ ] Isolation
1313

1414
## Technical Facts
1515

@@ -38,12 +38,22 @@ npm i @bitair/concurrent.js
3838

3939
## Usage
4040

41+
At its highest level of design, Concurrent.js is a dynamic module importer that loads a module into a web worker:
42+
4143
```js
4244
import { concurrent } from '@bitair/concurrent.js'
45+
// In Deno
46+
// import { concurrent } from 'https://deno.land/x/[email protected]/mod.ts'
47+
48+
// Import a module
49+
const MyModule = concurrent.import(new URL('./sample_module.js', import.meta.url))
50+
// In a CommonJS module
51+
// const MyModule = concurrent.import(path.join(__dirname, 'sample_module.js'))
4352

44-
// Import and load a JS module into a worker
45-
const { SampleObject, sampleFunction } =
46-
await concurrent.import(new URL('./sample_module.js', import.meta.url)).load()
53+
// Load it into a web worker
54+
const { SampleObject, sampleFunction } = await MyModule.load()
55+
// Load it into another web worker
56+
// const { SampleObject: SampleObject2, sampleFunction: sampleFunction2 } = await MyModule.load()
4757

4858
// Run a function
4959
const result = await sampleFunction(/*...args*/)
@@ -66,17 +76,59 @@ await concurrent.terminate()
6676
## Samples
6777
6878
- Browser
79+
6980
- [Basic usage](./apps/sample/browser/)
70-
- [Tensorflow.js](./apps/sample/browser-tensorflow/)
81+
- [Tensorflow.js usage](./apps/sample/browser-tensorflow/)
82+
83+
- Node & Bun
7184
72-
- Node
7385
- [Basic usage](./apps/sample/node/)
7486
7587
- Deno
7688
- [Basic usage](./apps/sample/deno/)
7789
90+
## Benchmark
91+
92+
The following results demonstrate the average execution time and CPU usage of running 10 concurrent calculations (10 iterations) of the factorial of 50,000 on various JavaScript runtime environments (RTEs). These calculations were performed on a Quad-core AMD APU with a base clock rate of 2.2GHz within a freshly installed isolated Ubuntu VM.
93+
94+
95+
(There are 213,237 digits in the factorial of 50,000)
96+
97+
| | RTE | JS Engine | Execution Time | CPU Usage |
98+
|---|---------------------|----------------------|------------------------|-----------|
99+
| 1 | **Deno** (v1.40) | V8 | 7.9168s | 100% |
100+
| 2 | **Chrome*** (v121.0) | V8 | 7.919s | 100% |
101+
| 3 | **Node** (v20.11) | V8 | 8.117s | 100% |
102+
| 4 | **Servo** (v0.0.1-c94d584) | SpiderMonkey | 31.267s | 99% |
103+
| 5 | **LibreWolf** (122.0) | SpiderMonkey | 35.417s | 92% |
104+
| 6 | **Firefox*** (v125.0) | SpiderMonkey | 49.061s | 95% |
105+
| 7 | **Bun** (v1.0.26) | JavaScriptCore | 51.502s | 99% |
106+
| 8 | **GNOME Web** (v45.2) | JavaScriptCore | 59.058s | 75% |
107+
108+
* A headless environment was used for benchmarking.
109+
110+
To benchmark Node, Deno, Bun RTEs as well as Chrome and Firefox browsers use the [benchmarking app](./apps/benchmark/):
111+
```bash
112+
git clone https://github.com/bitair-org/concurrent.js.git
113+
cd concurrent.js/apps/benchmark
114+
npm i
115+
npm start # This command starts a web server required by the headless browsers. Do not open the http://127.0.0.1:8080 address
116+
npm run benchmark
117+
```
118+
119+
For benchmarking other browsers, use the [browser basic usage sample](./apps/sample/browser)
120+
```bash
121+
git clone https://github.com/bitair-org/concurrent.js.git
122+
cd concurrent.js/apps/sample/browser
123+
npm i
124+
npm start # Open the http://127.0.0.1:8080 address in the target browser
125+
```
126+
127+
78128
## Parallelism
79129
130+
To run each function call or object instance on a separate CPU core, the `load` method of the imported module must be called for each function call or object instance individually:
131+
80132
```js
81133
import { concurrent } from '@bitair/concurrent.js'
82134

@@ -97,6 +149,7 @@ await concurrent.terminate()
97149
```
98150
99151
## Reactive Concurrency
152+
The reactive concurrency feature provides a bidirectional channel for messaging. A message can be replied to by returning a value:
100153
101154
`services/index.mjs`
102155
@@ -125,8 +178,7 @@ export async function reactiveAdd(channel /*: IChannel */) {
125178
```js
126179
import { concurrent, Channel } from '@bitair/concurrent.js'
127180

128-
const { reactiveAdd } =
129-
await concurrent.import(new URL('./services/index.mjs', import.meta.url)).load()
181+
const { reactiveAdd } = await concurrent.import(new URL('./services/index.mjs', import.meta.url)).load()
130182

131183
const channel = new Channel((onmessage, postMessage) => {
132184
const arr = [1, 2, 3, 4]
@@ -171,14 +223,17 @@ concurrent.config(settings: ConcurrencySettings): void
171223
Configures the global settings of Concurrent.js.
172224
173225
- `settings: ConcurrencySettings`
226+
174227
- `settings.maxThreads: number [default=1]`
175-
228+
176229
The maximum number of available threads to be spawned.
230+
177231
- `settings.threadIdleTimeout: number | typeof Infinity [default=Infinity]`
178-
232+
179233
Number of minutes before Concurrent.js terminates an idle thread.
234+
180235
- `settings.minThreads: number [default=0]`
181-
236+
182237
The number of threads created when Concurrent.js starts and kept alive to avoid thread recreation overhead.
183238
184239
```ts
@@ -190,7 +245,6 @@ Terminates Concurrent.js.
190245
- `force?: boolean [Not implemented]`
191246
Forces Concurrent.js to exit immediately without waiting for workers to finish their tasks.
192247
193-
194248
```ts
195249
class Channel implements IChannel
196250
```
@@ -204,6 +258,7 @@ Used to send/receive messages to/from functions and methods (instance or static)
204258
- ```ts
205259
onmessage(handler: (name: string | number, ...data: unknown[]) => unknown): void
206260
```
261+
207262
Sets the event handler for receiving a message. The handler should return a value if a reply is required for the message.
208263

209264
- ```ts
@@ -213,4 +268,5 @@ Used to send/receive messages to/from functions and methods (instance or static)
213268

214269
## License
215270

216-
[MIT License](./LICENSE)
271+
[MIT License](./LICENSE)
272+

apps/benchmark/deno.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"imports": {
3+
"extra-bigint": "npm:extra-bigint@^1.1.10"
4+
}
5+
}

apps/benchmark/deno.lock

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)