-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed2d5ca
commit a651439
Showing
3 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,54 @@ | ||
# parry | ||
A simple way of running functions in their own WebWorker. Mainly for deno | ||
|
||
A simple way of running functions in their own WebWorker that works with | ||
[deno](https://deno.land/). | ||
|
||
## Usage | ||
|
||
The following function will run in it's own WebWorker, enabling threading and | ||
therefor not blocking the main event loop. This becomes very useful for | ||
parallelization of certain computations or just a way of running a function and | ||
not blocking the main event loop while it runs. | ||
|
||
```ts | ||
import { parry } from "https://deno.land/x/parry/mod.ts"; | ||
|
||
async function countSerial(limit: number): Promise<void> { | ||
for (let i = 0; i < limit; i++) {} | ||
} | ||
|
||
console.time("4x CountSerial"); | ||
await Promise.all([ | ||
countSerial(1e9), | ||
countSerial(1e9), | ||
countSerial(1e9), | ||
countSerial(1e9), | ||
]); | ||
console.timeEnd("4x CountSerial"); | ||
|
||
const threads = [ | ||
parry(countSerial), | ||
parry(countSerial), | ||
parry(countSerial), | ||
parry(countSerial), | ||
]; | ||
|
||
console.time("4x CountParallel"); | ||
await Promise.all([ | ||
threads[0](1e9), | ||
threads[1](1e9), | ||
threads[2](1e9), | ||
threads[3](1e9), | ||
]); | ||
console.timeEnd("4x CountParallel"); | ||
|
||
parry.close(); | ||
``` | ||
|
||
The parallelized version is 4x faster than the serial version of the count | ||
function: | ||
|
||
``` | ||
4x CountSerial: 1885ms | ||
4x CountParallel: 509ms | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters