{"id":13725865,"url":"https://github.com/developit/task-worklet","last_synced_at":"2025-04-09T14:06:27.285Z","repository":{"id":66020109,"uuid":"149490658","full_name":"developit/task-worklet","owner":"developit","description":"Task Worklet: explainer, polyfill and demos.","archived":false,"fork":false,"pushed_at":"2021-08-17T20:23:48.000Z","size":60,"stargazers_count":275,"open_issues_count":4,"forks_count":4,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-04-01T22:10:04.131Z","etag":null,"topics":["threading","threadpool","worker","worker-pool","worklet"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/developit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-09-19T17:53:03.000Z","updated_at":"2024-04-17T08:53:55.000Z","dependencies_parsed_at":"2023-03-05T09:15:34.897Z","dependency_job_id":null,"html_url":"https://github.com/developit/task-worklet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Ftask-worklet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Ftask-worklet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Ftask-worklet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Ftask-worklet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developit","download_url":"https://codeload.github.com/developit/task-worklet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248054227,"owners_count":21039952,"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":["threading","threadpool","worker","worker-pool","worklet"],"created_at":"2024-08-03T01:02:38.073Z","updated_at":"2025-04-09T14:06:27.237Z","avatar_url":"https://github.com/developit.png","language":"JavaScript","readme":"# Task Worklet\n\nA polyfill for Task Worklet - a proposed API for defining and invoking coordinated, threadpooled background tasks with minimal transfer overhead.\n\n## Motivation\n\nA lot of what we do in modern web applications touches the DOM in some way.\nImproving the performance of DOM-bound code is difficult because issues generally stem from layout and paint cost rather than actual script execution overhead.\nTask Worklet attempts to define a highly ergonomic way to offload all of the work an application needs to do that _doesn't_ rely on the DOM, while making it incredibly easy to move data between the UI thread and background threads.\n\nIn addition to ergonomics, the design of Task Worklet allows for an implicit data flow graph to be formed based on how tasks are linked together to form dependencies on one another.  When combined with pooling and a centralized registry for task processors, this enables an algorithm to distribute work across multiple threads, automatically maximizing concurrency and minimizing transfer overhead.\n\n**Demo:** [Realtime JS compilation, bundling \u0026 compression](https://task-worklet-demo.glitch.me/)\n\n## Usage\n\nFirst, install the script via `npm install task-worklet` or grab it from [unpkg](https://unpkg.com/task-worklet).\n\nBy default, the `task-worklet` ships as an npm module that exports the `TaskQueue` interface.\n\nIf you'd prefer to \"install\" it as `window.TaskQueue`, go for `task-worklet/polyfill`:\n\n```html\n\u003cscript src=\"https://unpkg.com/task-worklet/polyfill\"\u003e\u003c/script\u003e\n```\n\nOnce you have it imported/installed, we can start interacting with the `TaskQueue`:\n\n```js\n// create a queue a max threadpool size of 1:\nconst queue = new TaskQueue();\n\n// add a Task Worklet:\nqueue.addModule('/fetch-worklet.js').then(() =\u003e { /* loaded */ })\n\nconst task = queue.postTask('fetch', 'https://example.com/data.json');\n\nconsole.log(task.state);  // pending\n\nawait sleep(1);\n\nconsole.log(task.state);  // scheduled\n\n// now we'll ask for the result back. This bumps up the priority\n// of the task and sends its result to the main thread once complete:\nconst result = await task.result;\nconsole.log(result)  // { ..some data.. }\n```\n\n**Here's the key:**  `task.result` is a lazy getter. If you don't ask for a Task's result by accessing `.result`, it will be kept in whatever background thread ran the task until it's needed.\n\nWhy keep task results in the thread that ran the task?\nThat's the best part: we can pass `Task` instances to `postTask()`, and instead of \"pulling\" the result of a task back to the main thread and sending it on to the thread that's running that second task, the second task will be routed to the thread that already has the first's result waiting:\n\n```js\nconst data = q.postTask('fetch', 'https://example.com/data.json');\n\nconst subset = q.postTask('filter', data, ['items', 'count']);\n\n// we only end up with the subsetted data on the main thread:\nconsole.log(await subset.result);\n```\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Ftask-worklet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelopit%2Ftask-worklet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Ftask-worklet/lists"}