{"id":29022024,"url":"https://github.com/jsweb/worker","last_synced_at":"2025-06-26T02:37:31.610Z","repository":{"id":57122600,"uuid":"287408711","full_name":"jsweb/worker","owner":"jsweb","description":"JavaScript module to parallel process data by dynamic multi-thread workers","archived":false,"fork":false,"pushed_at":"2020-08-18T01:03:26.000Z","size":393,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-20T09:55:36.970Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsweb.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}},"created_at":"2020-08-14T00:41:00.000Z","updated_at":"2020-08-18T01:03:24.000Z","dependencies_parsed_at":"2022-08-24T14:59:30.745Z","dependency_job_id":null,"html_url":"https://github.com/jsweb/worker","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/jsweb/worker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsweb%2Fworker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsweb%2Fworker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsweb%2Fworker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsweb%2Fworker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsweb","download_url":"https://codeload.github.com/jsweb/worker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsweb%2Fworker/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261178424,"owners_count":23120824,"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-06-26T02:37:30.876Z","updated_at":"2025-06-26T02:37:31.595Z","avatar_url":"https://github.com/jsweb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @jsweb/worker\n\nJavaScript module to parallel process data through dynamic multi-thread workers.\n\n## Installation\n\n### NPM or Yarn\n\nThe most common way used for the modern web applications development:\n\n`npm i -S @jsweb/worker`\n\n`yarn add @jsweb/worker`\n\nAnd at your JS you can import the module that fits your environment:\n\n```javascript\n// ES6+ frontend web dev to use worker at the browser\nimport worker from '@jsweb/worker'\n\n// ES6+ backend dev to use worker in Node.js\nimport worker from '@jsweb/worker/dist/node.mjs'\n\n// CommonJS backend dev to use worker in Node.js\nconst worker = require('@jsweb/worker/dist/node.js')\n```\n\nYes, it works in Node.js, but you must to import/require the correct module for your dev environment.\n\n### CDN\n\nIf you prefer, you can get it directly from Unpkg CDN to use it in web frontend.\n\n```html\n\u003cscript src=\"https://unpkg.com/@jsweb/worker\"\u003e\u003c/script\u003e\n```\n\nBy including the script in your page as traditional JS, the `jsWebWorker` function becomes available at the global scope.\n\nThis `main` exported module is in UMD format and targets browser.\n\nBut if you want to use a modern ES6+ module, you can do this:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import worker from 'https://unpkg.com/@jsweb/worker/dist/browser.js'\n\n  // Your code goes here...\n\u003c/script\u003e\n```\n\n## Usage example\n\n```javascript\nimport worker from '@jsweb/worker'\n\nlet result\n\nconst data = [...aLotOfItemsHere]\n\nworker(data)\n  .env('two', 2)\n  .map((item) =\u003e item.value ** self.env.two)\n  .reduce((value, sum) =\u003e value + sum, 0)\n  .finally((sum) =\u003e (result = sum))\n```\n\n**Important:**\n\nNotice the `self` thing into the code!\n\nAny function task running at the Worker must refer to the environment as `self` to access other context resources, except for globals.\n\n## The `worker`\n\n### Instance\n\nThe `worker` function returns a high level object instance that works like `Promise`, with `async` methods.\n\nIn fact, it uses a `Promise` internaly to guide the entire process data flow when methods are called.\n\nYou can chain or `async/await` them to get the results.\n\nThe `worker` instance receive 2 arguments:\n\n1. data or value to process (required)\n2. a number to setup concurrent threads to process data (optional)\n\n```javascript\nworker(data, 4).exec(...)\n```\n\nThe worker tries to detect how much \"hardware concurrency threads\" are available to use, or use the default: `2`.\n\n### Environment (scope)\n\nAt the browser, Workers run in parallel hardware threads, detached from main JS application thread.\n\nIt means you don't have access to all props and features available at the global JS scope.\n\nIf you need to know more about this, try to start here: [Web Workers API : Functions and interfaces available in workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#Functions_and_interfaces_available_in_workers).\n\nAt the Node.js environment, workers use `child_process`. To know more about this, go to [Node.js API: child_process](https://nodejs.org/api/child_process.html)\n\n## Methods\n\nAll methods are asyncronous and return the `worker` instance to chain other methods, except the `.finally` method, that ends the flow `worker` and returns just a native `Promise`.\n\n### .env(key, value)\n\nSets a Worker environment constant attached to 'self.env' object.\n\n- `key` must to be a valid object key\n- `value` can be any data to be available at Worker environment\n\nInside your tasks functions, you can refer to these constants through 'self.env' object.\n\n```javascript\nworker(data)\n  .env('x', 5)\n  .env('y', 25)\n  .exec((value) =\u003e value * self.env.x * self.env.y)\n```\n\n### .imports(...args)\n\nImports code to use at Worker enviroment like functions or scripts.\n\nAccepts any number of named functions or scripts URLs.\n\n```javascript\nconst moment = 'https://momentjs.com/downloads/moment.min.js'\n\nfunction howManyTimeAgo(dt) {\n  return self.moment(dt).fromNow()\n}\n\nfunction ageCalc({ birthDate }) {\n  return self.howManyTimeAgo(birthDate)\n}\n\nworker(data).imports(moment, howManyTimeAgo).exec(ageCalc)\n```\n\nIt's just a \"dumb\" example... But it's important to remember about Worker scope!\n\n### .unpkg(...args)\n\nImport NPM modules from [UNPKG CDN](https://unpkg.com/) to use at Worker environment.\n\nAccepts any number of string arguments to identify modules.\n\n```javascript\nfunction createKeysForAllItems(item) {\n  item.key = self.randkey.uuid()\n\n  return item\n}\n\nworker(data).unpkg('randkey').map(createKeysForAllItems)\n```\n\n### .exec(task)\n\nExecutes a task function in a single parallel thread.\n\n```javascript\nworker(data)\n  .env('x', 5)\n  .env('y', 25)\n  .exec((value) =\u003e value * self.env.x * self.env.y)\n```\n\n### .map(task)\n\nExecutes a multi-thread `Array.map` method.\n\n```javascript\nworker(data)\n  .env('two', 2)\n  .map((item) =\u003e item.value ** self.env.two)\n```\n\n### .reduce(task, arg)\n\nExecutes a multi-thread `Array.reduce` method.\n\n```javascript\nworker(data).reduce((value, sum) =\u003e value + sum, 0)\n```\n\n### .filter(task)\n\nExecutes a single parallel thread `Array.filter` method.\n\n```javascript\nworker(data).filter((value) =\u003e value.includes('OK'))\n```\n\n### .sort(task)\n\nExecutes a single parallel thread `Array.sort` method.\n\n```javascript\nworker(data).sort((a, b) =\u003e a - b)\n```\n\n### .flatMap(task)\n\nExecutes a multi-thread `Array.flatMap` method.\n\n```javascript\nconst data = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n  // ...\n]\n\nworker(data)\n  .env('two', 2)\n  .faltMap((item) =\u003e item.value ** self.env.two)\n```\n\n### .then(task)\n\nPromise-like async step.\n\nUnlike the native `Promise.then`, which returns a `Promise`, this method returns the `worker` instance to chain another methods.\n\n```javascript\nworker(data)\n  .filter((value) =\u003e value.includes('OK'))\n  .then(doSomeThing)\n```\n\n### .finally(task)\n\nPromise-like async **last** step.\n\nLike the native `Promise.finally`, this method returns a `Promise` and stops the `worker` instance flow.\n\nYou can't chain other `worker` methods after this.\n\n```javascript\nworker(data)\n  .filter((value) =\u003e value.includes('OK'))\n  .then(doSomeThing)\n  .finally(doLastThing)\n```\n\n## How this module works?\n\nComing soon...\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsweb%2Fworker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsweb%2Fworker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsweb%2Fworker/lists"}