{"id":25287966,"url":"https://github.com/paberr/wasmworker","last_synced_at":"2025-10-27T22:30:38.256Z","repository":{"id":264389244,"uuid":"893232923","full_name":"paberr/wasmworker","owner":"paberr","description":"Dispatching tasks to a WebWorker without `SharedArrayBuffers`.","archived":false,"fork":false,"pushed_at":"2024-12-04T00:54:13.000Z","size":263,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-05T09:57:16.150Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paberr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-23T22:00:35.000Z","updated_at":"2024-11-30T05:58:17.000Z","dependencies_parsed_at":"2024-11-29T18:31:29.058Z","dependency_job_id":null,"html_url":"https://github.com/paberr/wasmworker","commit_stats":null,"previous_names":["paberr/webworker-test","paberr/wasmworker"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paberr%2Fwasmworker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paberr%2Fwasmworker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paberr%2Fwasmworker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paberr%2Fwasmworker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paberr","download_url":"https://codeload.github.com/paberr/wasmworker/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238563733,"owners_count":19492976,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-12T22:52:46.385Z","updated_at":"2025-10-27T22:30:32.970Z","avatar_url":"https://github.com/paberr.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# wasmworker\n`wasmworker` is a library that provides easy access to parallelization on web targets when compiled to WebAssembly using [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen).\nIn contrast to many other libraries like [wasm-bindgen-rayon](https://github.com/RReverser/wasm-bindgen-rayon), this library does not require SharedArrayBuffer support.\n\n- [Usage](#usage)\n  - [Setting up](#setting-up)\n  - [Outsourcing tasks](#outsourcing-tasks)\n    - [WebWorker](#webworker)\n    - [WebWorkerPool](#webworkerpool)\n    - [Iterator extension](#iterator-extension)\n  - [Feature detection](#feature-detection)\n- [FAQ](#faq)\n\n## Usage\nThe library consists of two crates:\n- `wasmworker`: The main crate that also offers access to the webworker, as well as the worker pool and iterator extensions.\n- `wasmworker-proc-macro`: This crate is needed to expose functions towards the web workers via the `#[webworker_fn]` macro.\n\n### Setting up\nTo use this library, include both dependencies to your `Cargo.toml`.\n\n```toml\n[dependencies]\nwasmworker = \"0.1\"\nwasmworker-proc-macro = \"0.1\"\n```\n\nThe `wasmworker` crate comes with a default feature called `serde`, which allows running any function on a web worker under the following two conditions:\n1. The function takes a single argument, which implements `serde::Serialize + serde::Deserialize\u003c'de\u003e`.\n2. The return type implements `serde::Serialize + serde::Deserialize\u003c'de\u003e`.\n\nWithout the `serde` feature, only functions with the type `fn(Box\u003c[u8]\u003e) -\u003e Box\u003c[u8]\u003e` can be run on a worker.\nThis is useful for users that do not want a direct serde dependency. Internally, the library always uses serde, though.\n\nYou can then start using the library without further setup.\nIf you plan on using the global `WebWorkerPool` (using the iterator extensions or `worker_pool()`), you can *optionally* configure this pool:\n```rust\n// Importing it publicly will also expose the function on the JavaScript side.\n// You can instantiate the pool both via Rust and JS.\npub use wasmworker::{init_worker_pool, WorkerPoolOptions};\n\nasync fn startup() {\n    init_worker_pool(WorkerPoolOptions {\n        num_workers: Some(2), // Default is navigator.hardwareConcurrency\n        ..Default::default()\n    }).await;\n}\n```\n\n### Outsourcing tasks\nThe library offers three ways of outsourcing function calls onto concurrent workers:\n1. `WebWorker`: a single worker, to which tasks can be queued to.\n2. `WebWorkerPool`: a pool of multiple workers, to which tasks are distributed.\n3. `par_map`: an extension to regular iterators, which allows to execute a function on every element of the iterator in parallel using the default worker pool.\n\nAll approaches require the functions that should be executed to be annotated with the `#[webworker_fn]` macro.\nThis macro ensures that the functions are available to the web worker instances:\n\n```rust,ignore\nuse serde::{Deserialize, Serialize};\nuse wasmworker_proc_macro::webworker_fn;\n\n/// An arbitrary type that is (de)serializable.\n#[derive(Serialize, Deserialize)]\npub struct VecType(Vec\u003cu8\u003e);\n\n/// A sort function on a custom type.\n#[webworker_fn]\npub fn sort_vec(mut v: VecType) -\u003e VecType {\n    v.0.sort();\n    v\n}\n```\n\nWhenever we want to execute a function, we need to pass the corresponding `WebWorkerFn` object to the worker.\nThis object describes the function to the worker and can be safely obtained via the `webworker!()` macro:\n\n```rust,ignore\nuse wasmworker::webworker;\n\nlet ww_sort = webworker!(sort_vec);\n```\n\n#### WebWorker\nWe can instantiate our own workers and run functions on them:\n```rust,ignore\nuse wasmworker::{webworker, WebWorker};\n\nlet worker = WebWorker::new(None).await;\nlet res = worker.run(webworker!(sort_vec), \u0026VecType(vec![5, 2, 8])).await;\nassert_eq!(res.0, vec![2, 5, 8]);\n```\n\n#### WebWorkerPool\nMost of the time, we probably want to schedule tasks to a pool of workers, though.\nThe default worker pool is instantiated on first use and can be configured using `init_worker_pool()` as described above.\nIt uses a round-robin scheduler (with the second option being a load based scheduler), a number of `navigator.hardwareConcurrency` separate workers, and the default inferred path.\n\n```rust,ignore\nuse wasmworker::{webworker, worker_pool};\n\nlet worker_pool = worker_pool().await;\nlet res = worker_pool.run(webworker!(sort_vec), \u0026VecType(vec![5, 2, 8])).await;\nassert_eq!(res.0, vec![2, 5, 8]);\n```\n\n#### Iterator extension\nInspired by [Rayon](https://github.com/rayon-rs/rayon), this library also offers a (much simpler and less powerful) method for iterators.\nThis functionality automatically parallelizes a map operation on the default worker pool.\n\n```rust,ignore\nuse wasmworker::iter_ext::IteratorExt;\n\nlet some_vec = vec![\n    VecType(vec![5, 2, 8]),\n    // ...\n];\nlet res: Vec\u003cVecType\u003e = some_vec.iter().par_map(webworker!(sort_vec)).await;\n```\n\n## FAQ\n1. _Why would you not want to use SharedArrayBuffers?_\n\n    The use of SharedArrayBuffers requires cross-origin policy headers to be set, which is not possible in every environment.\n    Moreover, most libraries that rely on SharedArrayBuffers, also require a nightly version of Rust at the moment.\n    An important goal of this library is to remove these requirements.\n\n2. _Which `wasm-bindgen` targets are supported?_\n\n    So far, this library has only been tested with `--target web`.\n    Other targets seem to generally be problematic in that the wasm glue is inaccessible or paths are not correct.\n    Both the `Worker` and `WebWorkerPool` have an option to set a custom path, which should make it possible to support other targets dynamically, though.\n\n3. _Can I use bundlers?_\n\n    The usage of bundlers has not been officially tested. This might be added in the future.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaberr%2Fwasmworker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaberr%2Fwasmworker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaberr%2Fwasmworker/lists"}