{"id":22775238,"url":"https://github.com/than-dev/increase-node-performance","last_synced_at":"2026-04-18T13:33:32.452Z","repository":{"id":122461847,"uuid":"407321961","full_name":"than-dev/increase-node-performance","owner":"than-dev","description":"Increasing nodejs performance with cluster and work threads. Explanations, use cases and benchmarks, enjoy it.","archived":false,"fork":false,"pushed_at":"2022-01-03T00:28:52.000Z","size":569,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-30T13:14:43.965Z","etag":null,"topics":["backend","benchmark","clustering","nodejs","performance","worker-threads"],"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/than-dev.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-16T21:38:34.000Z","updated_at":"2022-01-03T00:28:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"4ca4efd9-3267-4361-8637-8fce6b4b9e16","html_url":"https://github.com/than-dev/increase-node-performance","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/than-dev/increase-node-performance","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/than-dev%2Fincrease-node-performance","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/than-dev%2Fincrease-node-performance/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/than-dev%2Fincrease-node-performance/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/than-dev%2Fincrease-node-performance/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/than-dev","download_url":"https://codeload.github.com/than-dev/increase-node-performance/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/than-dev%2Fincrease-node-performance/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264995105,"owners_count":23694883,"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":["backend","benchmark","clustering","nodejs","performance","worker-threads"],"created_at":"2024-12-11T18:26:30.864Z","updated_at":"2026-04-18T13:33:27.432Z","avatar_url":"https://github.com/than-dev.png","language":"JavaScript","readme":"\u003ch2 align=\"center\"\u003e Enhance NodeJS Performance\n\u003cdiv align=\"center\"\u003e\n\u003cbr\u003e\n\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](/LICENSE)\n\n\u003c/div\u003e\n\n\u003cbr\u003e\n\n## ➰ Two different ways to do it\n\n- (Recommended) Node 'Cluster' mode\n- (Experimental) Worker Threads\n  \n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## 🐊 Start\n\nFirst we need to understand the default application flux, that is:\n\n```\nRequest ----\u003e Node Server ----\u003e Response\n```\n\nIt causes a block in all the event loop, this way, in request process much longs, we can lose performance and get worse the user experience.\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## 🛠️ Cluster Module\n\nThe clustering process creates and manages some nodejs event loop instances, winning this way mor threads to execute our code, action that will increase our speed. This instances are monitored by the Cluster Manager\n\nFollows a visual representation:\n\n\u003cdiv align=\"center\"\u003e\n\u003cimg src=\"https://miro.medium.com/max/412/1*1dzWfKzhph6oFhPjqj6x2g.png\"\u003e\n\u003c/div\u003e\n\n\u003cbr\u003e\n\nThis basically call a lot of times the server index file, in the first execution, it creates the Cluster Manager and then it will create some new workers instances (child process). The command to create children process: \n\n```\ncluster.fork()\n```\n\n\u003cbr\u003e\n\nCommand to verify if the process is the primary(first execution):\n\n```\ncluster.isPrimary(): Boolean\n```\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n### Benefits\nUsing it you can break your server flux in most pieces, this way you will process more then one request at a time.\n\n\u003cbr\u003e\n\n\n### Disadvantages and Not Usecases\nIf we increase the number of children process, will arrive a moment that our performance will decrease, because all machines have a limit of processing, because it, is recommendable we use this logic:\n\n```\nconst numCPUs = cpus().length;\n\n for (let i = 0; i \u003c numCPUs; i++) {\n    cluster.fork();\n}\n```\n\n\u003cbr\u003e\n\n### Conclusion\nTo use this feature we need to be wary before, you can fill free to do your tests and benchmarks using the file _cluster.js as base. Prefer to use it when you need big processing in the request.\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## 🔨 Worker Threads\nJust use it if you have a big business logic that can take much time.\n\nOur app communicate with the webworker through this flux:\n\nOur app postMessage, and the webWorker stay listening an event 'onMessage', like the nodejs Event Emitter. \u003cbr\u003e\nThe applied example in the file _workers-threads.js is at nodejs documentation, see it to more details.\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n### 🛸 Author: Nathan Cotrim - MIT License\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthan-dev%2Fincrease-node-performance","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthan-dev%2Fincrease-node-performance","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthan-dev%2Fincrease-node-performance/lists"}