{"id":13679427,"url":"https://github.com/yankouskia/hurried","last_synced_at":"2025-04-09T07:07:33.719Z","repository":{"id":34892735,"uuid":"174623076","full_name":"yankouskia/hurried","owner":"yankouskia","description":"⚡️ JavaScript library for parallel code execution :twisted_rightwards_arrows:","archived":false,"fork":false,"pushed_at":"2023-01-08T20:23:14.000Z","size":159,"stargazers_count":254,"open_issues_count":2,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-02T06:08:06.825Z","etag":null,"topics":["javascript","nodejs","parallel-programming"],"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/yankouskia.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2019-03-08T23:07:43.000Z","updated_at":"2025-02-28T18:05:23.000Z","dependencies_parsed_at":"2023-01-15T10:00:06.059Z","dependency_job_id":null,"html_url":"https://github.com/yankouskia/hurried","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/yankouskia%2Fhurried","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankouskia%2Fhurried/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankouskia%2Fhurried/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankouskia%2Fhurried/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yankouskia","download_url":"https://codeload.github.com/yankouskia/hurried/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247994121,"owners_count":21030050,"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":["javascript","nodejs","parallel-programming"],"created_at":"2024-08-02T13:01:05.470Z","updated_at":"2025-04-09T07:07:33.687Z","avatar_url":"https://github.com/yankouskia.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","TypeScript"],"sub_categories":[],"readme":"[![CircleCI](https://circleci.com/gh/yankouskia/hurried.svg?style=shield)](https://circleci.com/gh/yankouskia/hurried) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/yankouskia/hurried/pulls) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yankouskia/hurried/blob/master/LICENSE)\n\n[![NPM](https://nodei.co/npm/hurried.png?downloads=true)](https://www.npmjs.com/package/hurried)\n\n# hurried\n\nJavaScript library for ~~concurrent~~ **_parallel_** code execution.\n\n## Motivation\n\nLibrary is built on top of new [Worker Threads](https://nodejs.org/api/worker_threads.html) functionality, which is introduced in [Node 10.5.0](https://nodejs.org/en/blog/release/v10.5.0/).\nThere is an existing API for [forking processes in Node.js](https://nodejs.org/api/child_process.html). That solution is not the best, because forking a process is pretty expensive operation in terms of resources, which could be very slow. Creating worker thread is much faster and requires less resources.\n\n## How to use\n\nTo install library:\n\n```sh\n# yarn\nyarn add hurried\n\n# npm\nnpm install hurried --save\n```\n\nLibrary is designed to create **independent JavaScript execution thread** for parallel execution. Most Node.js APIs are available inside of it. To create thread:\n\n```js\n// ES6 modules\nimport { Thread } from 'hurried';\n\n// CommonJS modules\nconst { Thread } = require('hurried');\n\n// To create execution thread for any file:\nconst threadFromFile = Thread.fromFile(path.resolve(__dirname, 'test.js'));\n\n// To create execution thread from any script:\nconst threadFromScript = Thread.fromScript(`\n  for (let i = 0; i \u003c 10 ** 9; i++) {\n    // some logic\n  }\n`);\n\n```\n\nCreating new `Thread` runs `script`/`module` immediately. Such approach is useful for creating several execution threads for issues, which require CPU intensive tasks.\n\n\n## Run specific function\n\n`hurried` allows you to specify functions in your code, which will be accessible for calling from the main thread.\nTo specify such function:\n\n```js\nconst { makeExecutable } = require('hurried');\n\nfunction slowFunction(...params) {\n  // some slow code which requires intensive blocking CPU work\n  return params;\n}\n\nmodule.exports.slowFunction = slowFunction;\n\nmakeExecutable(slowFunction, 'slow');\n```\n\n`makeExecutable` will not do anything, if it will be ran directly from main thread. If it will be used in another execution thread it will make that function `executable`.\n\nTo use that from main thread:\n\n```js\nconst { Thread } = require('hurried');\n\n(async () =\u003e {\n  const thread = Thread.fromFile(path.resolve(__dirname, 'slow.js'));\n  const slowResult = await thread.run('slow', 'param', 1);\n\n  thread.terminate();\n})()\n```\n\n\n## API\n\n### Thread\n\n`static Thread.setMaxListeners(count: number): void`\n\nThe same as [Node.js Event Emitter setMaxListeners](https://nodejs.org/api/events.html#events_emitter_setmaxlisteners_n) to help finding / preventing memory leaks.\n\n`static Thread.isMainThread(): boolean`\n\nReturns true, if code is not running inside Worker.\n\n`static Thread.fromFile(filename: string, options: OptionsType): Thread`\n\nCreates independent JavaScript execution thread from module.\n\n`static Thread.fromScript(script: string, options: OptionsType): Thread`\n\nCreates independent JavaScript execution thread from code script.\n\n`thread.run(name [, ...params: any[]]): Function\u003creturn|resolve\u003e`\n\nProvides ability to run specific function from independent JavaScript execution thread.\nAny serializable params could be used.\nFunction which is called from another thread could return any serializable value or `Promise`, which is resolved with serializable value.\n\n`thread.terminate([callback]): void`\n\nStop all JavaScript execution in the worker thread as soon as possible.\ncallback is an optional function that is invoked once this operation is known to have completed.\n\n\n### makeExecutable\n\n`makeExecutable(fn: Function, name: String): void`\n\nProvides ability to make function callable and executable inside independent JavaScript execution thread from main thread.\nFunction should return any serializable value or `Promise`, which is resolved with that value.\n\n\n### OptionsType\n\n`env: Object`\n\nIf set, specifies the initial value of process.env inside the Worker thread. As a special value, worker.SHARE_ENV may be used to specify that the parent thread and the child thread should share their environment variables; in that case, changes to one thread’s process.env object will affect the other thread as well. Default: process.env.\n\n`execArgv: string[]`\n\nList of node CLI options passed to the worker. V8 options (such as --max-old-space-size) and options that affect the process (such as --title) are not supported. If set, this will be provided as process.execArgv inside the worker. By default, options will be inherited from the parent thread.\n\n`stdin: boolean`\n\nIf this is set to true, then worker.stdin will provide a writable stream whose contents will appear as process.stdin inside the Worker. By default, no data is provided.\n\n`stdout: boolean`\n\nIf this is set to true, then worker.stdout will not automatically be piped through to process.stdout in the parent.\n\n`stderr: boolean`\n\nIf this is set to true, then worker.stderr will not automatically be piped through to process.stderr in the parent.\n\n`workerData: any`\n\nAny JavaScript value that will be cloned and made available as require('worker_threads').workerData. The cloning will occur as described in the HTML structured clone algorithm, and an error will be thrown if the object cannot be cloned (e.g. because it contains functions).\n\n\n## Examples\n\nThere are several examples in projects, which could be helpful to start.\nExample could be found [here](https://github.com/yankouskia/hurried/tree/master/examples)\n\nRunning [this example](https://github.com/yankouskia/hurried/tree/master/examples/performance) allows to see how **fast** to run CPU blocking code in separate threads\n\n## Restriction\n\nAt least `Node.js 10.5.0` is required to run this library\n\n## Contributing\n\n`hurried` is open-source library, opened for contributions\n\n### Tests\n\n`jest` is used for tests. To run tests:\n\n```sh\nyarn test\n```\n\n### License\n\nhurried is [MIT licensed](https://github.com/yankouskia/hurried/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyankouskia%2Fhurried","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyankouskia%2Fhurried","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyankouskia%2Fhurried/lists"}