-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: leonid.polukhin <[email protected]>
- Loading branch information
1 parent
7c4582c
commit 007a3d4
Showing
6 changed files
with
79 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,12 +1,31 @@ | ||
import workerpool from 'workerpool' | ||
import { arrayBuffer } from 'stream/consumers'; | ||
import workerpool, { worker } from 'workerpool' | ||
|
||
// a deliberately inefficient implementation of the fibonacci sequence | ||
function fibonacci(n: number): number { | ||
if (n < 2) return n; | ||
return fibonacci(n - 2) + fibonacci(n - 1); | ||
} | ||
|
||
// As ArrayBuffer.prototype.detached is a rather recent feature it is not used here. | ||
function isDetached (buffer: ArrayBuffer) : boolean { | ||
try { | ||
const array = new Uint8Array (buffer); | ||
return false; | ||
} | ||
catch (error) { | ||
return true; | ||
} | ||
} | ||
|
||
function createArray(size: number) : boolean { | ||
const array = size > 0 ? new Uint8Array(size) : new Uint8Array(); | ||
workerpool.workerEmit(new workerpool.Transfer(array, [array.buffer])); | ||
return isDetached(array.buffer); | ||
} | ||
|
||
// create a worker and register public functions | ||
workerpool.worker({ | ||
fibonacci: fibonacci | ||
fibonacci: fibonacci, | ||
createArray: createArray | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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