{"id":17160068,"url":"https://github.com/arqex/worker-function","last_synced_at":"2025-04-13T14:08:58.496Z","repository":{"id":51523167,"uuid":"95360487","full_name":"arqex/worker-function","owner":"arqex","description":"Create functions that are executed inside of web workers and return promises.","archived":false,"fork":false,"pushed_at":"2021-05-11T11:55:01.000Z","size":29,"stargazers_count":13,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-13T14:08:42.726Z","etag":null,"topics":["inline","promise","promises","webworker","webworkers"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arqex.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-25T13:10:28.000Z","updated_at":"2023-11-07T02:28:46.000Z","dependencies_parsed_at":"2022-08-03T04:30:47.753Z","dependency_job_id":null,"html_url":"https://github.com/arqex/worker-function","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arqex%2Fworker-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arqex%2Fworker-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arqex%2Fworker-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arqex%2Fworker-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arqex","download_url":"https://codeload.github.com/arqex/worker-function/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248724629,"owners_count":21151561,"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":["inline","promise","promises","webworker","webworkers"],"created_at":"2024-10-14T22:23:14.370Z","updated_at":"2025-04-13T14:08:58.473Z","avatar_url":"https://github.com/arqex.png","language":"JavaScript","readme":"# worker-function\n\nCreate functions that are executed inside of web workers and return promises.\n\nAllows to create inline web workers without the need of creating new files for them. Have a look at the example:\n\n```js\nconst WorkerFunction = require('worker-function');\n\n// Let's create a new worker\nvar workerSum = WorkerFunction( function( arg1, arg2, done ){\n  // Worker execution can be async,\n  // don't forget to call `done`\n  setTimeout( () =\u003e done( arg1 + arg2 ), 2000 );\n});\n\n// workerSum is used in a new thread\n// and return a promise with the result\nworkerSum(2, 3).then( result =\u003e {\n  console.log( result ); // 5\n});\n```\n[See it working in a JSBin](https://jsbin.com/geqohac/edit?js,console).\n\n`worker-function` reinforce the usage of web workers as disposable resources. In the example, everytime `workerSum` is called, a new web worker is created to execute the function in its own thread. When the function `done` is called the web worker is terminated freeing memory.\n\nThe library is really lightweight, less than 1Kb minified.\n\n## Installation\n```\nnpm install worker-function\n```\n\nOr you can use [WorkerFunction.js](https://github.com/arqex/worker-function/blob/master/WorkerFunction.js) directly in the browser.\n\n## usage\nFunctions to be executed in a web worker are created by passing them to the `WorkerFunction`:\n\n```js\nvar workerFn = WorkerFunction( function Fn(arg1,arg2,...,argN,done){\n  // Calling done will resolve the promise with the result given\n  done( 'Hey there' );\n});\n```\n\nNow it's possible to call `workerFn` in the usual way, but it will be executed in its own thread, within a web worker. The execution will be isolated from the main browser thread so you can't use any of the variables defined outside of the function.\n\nIt's possible to pass any number of arguments needed to a worker function. In addition, `done` function is always passed as the last argument, and **it's mandatory to call it to send the result from the main thread**, resolving the promise and terminating the worker:\n\n```js\nworkerFn(arg1, arg2, ..., argN)\n  .then( result =\u003e {\n    console.log( result ); // 'Hey there'\n  })\n  .catch( err =\u003e {\n    // Any error inside the worker execution can\n    // be catched using the Promise's catch method\n    console.error( err );\n  })\n;\n```\n\n## Debugging\nYou can use your browser's dev tools to debug your worker functions. Try to add `debugger` to your function and the debugger will stop there:\n\n```js\nvar wf = WorkerFunction( done =\u003e {\n  debugger;\n  done('This function was stopped in the previous line.');\n});\n```\n\n## Compatibility\nMost of modern browsers support web workers, [see compatibility list](https://caniuse.com/#feat=webworkers).\n\nBut in case we need to run the code in browsers that don't support them, or in Node environments where web workers are not available, `worker-function` falls back running the functions in the main thread, so we can use the library with no compromise.\n\nThe only requirement of `worker-function` to work is to have `Promise`s available. So if we need our code to be compatible with old browers, we need to get sure we [polyfill promises](https://github.com/taylorhakes/promise-polyfill).\n\n\n## Performance\n`worker-function` treat web workers as disposable resources, so there is some time spent when we start a worker up.\n\nFortunatelly, that startup time is almost unperceivable in the modern browsers. In our benchmarks, running a worker-function scores almost exactly the same than running the function in the body of the `Promise.resolve` method:\n\n```\nChrome 81\n---\nWorker function x 67.73 ops/sec ±2.81% (56 runs sampled)\nPromise function x 68.78 ops/sec ±2.36% (50 runs sampled)\n\nFirefox 75\n---\nWorker function x 182 ops/sec ±2.11% (54 runs sampled)\nPromise function x 182 ops/sec ±2.43% (55 runs sampled)\n\nSafari 13\n---\nWorker function x 307 ops/sec ±1.61% (57 runs sampled)\nPromise function x 318 ops/sec ±1.29% (59 runs sampled)\n```\n\nThese benchmarks are available for anyone to run at [test.html](https://github.com/arqex/worker-function/blob/master/test.html).\n\n\n## License\n[Mit](LICENSE) © Javier Marquez\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farqex%2Fworker-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farqex%2Fworker-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farqex%2Fworker-function/lists"}