{"id":22775231,"url":"https://github.com/than-dev/is-node-single-thread","last_synced_at":"2025-03-30T13:14:43.485Z","repository":{"id":122461875,"uuid":"404358518","full_name":"than-dev/is-node-single-thread","owner":"than-dev","description":"It's a set of explanations and benchmark tests to answer the question, NodeJS is really single-thread? And how it really works in background.","archived":false,"fork":false,"pushed_at":"2021-09-08T16:28:57.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-05T14:48:50.741Z","etag":null,"topics":["article","callstack","event-loop","javascript","nodejs","thread","v8"],"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-08T13:29:45.000Z","updated_at":"2022-01-02T13:52:49.000Z","dependencies_parsed_at":"2024-06-12T02:05:23.728Z","dependency_job_id":null,"html_url":"https://github.com/than-dev/is-node-single-thread","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/than-dev%2Fis-node-single-thread","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/than-dev%2Fis-node-single-thread/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/than-dev%2Fis-node-single-thread/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/than-dev%2Fis-node-single-thread/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/than-dev","download_url":"https://codeload.github.com/than-dev/is-node-single-thread/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246320199,"owners_count":20758410,"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":["article","callstack","event-loop","javascript","nodejs","thread","v8"],"created_at":"2024-12-11T18:26:30.556Z","updated_at":"2025-03-30T13:14:43.465Z","avatar_url":"https://github.com/than-dev.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"\" rel=\"noopener\"\u003e\n \u003cimg src=\"https://www.luby.com.br/wp-content/uploads/2020/11/nodejs-luby.png\" alt=\"Project logo\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003ch3 align=\"center\"\u003eNodeJS Is Single Thread?\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## NodeJS\n\nNodeJS is a runtime that allow us to execute Javascript out of browser, to give us this experience, it use basically V8 engine (the same we see at browsers to interpret js code) and Libuv, with exception another dependencies like http, c++ bindings and core modules!\n\nSee nodejs behavior as simple way:\n\n\u003cbr\u003e\n\n\u003cimg src=\"https://telegra.ph/file/ed311508d749291f27132.png\"\u003e\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## V8\n\nThis is what interpret our js code, V8 has 70% of his code in C++, to connect C++ and javascript function, occurs a process.binding()!\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## Threads\n\nIn resume, Thread is the task that a given program performs, Thread of execution.\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## Event Loop\n\nNodejs is guided by events (Event Driven).Basically, the event loop is a single-thread, non-blocking, and asynchronous concurrent loop, It verifies some points to know if it can finish the process ou go on with it, this verification contains:\n\n- pending timers: setTimeout, setInterval...\n- pending OS tasks: like server listening to port\n- pending operations: like fs\n\nThe event loop is what really takes care of asynchronous code execution for the JS engines.\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## Call Stack\n\nWhenever a function is executed it enters the stack, which only executes one thing at a time, that is, the code after the one that is running needs to wait for the current function to execute before proceeding. Call Stack is a V8 feature, not directly from js, because it, in resume, NodeJS is single-thread, but we will see another points about it.\n\nWhen a function is called, it go to callStack, when finish it execution, go out.\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## Multi Threading\n\nBut NodeJS not is single thread? At true, who really is single-thread is the V8, to be possible execute async tasks NodeJS uses some APIs, some of them provided by the own OS. Who controls all this part of multiple threads, async and I/O is Libuv;\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## Libuv\n\nLibuv is an open-source lib writing in C, actually it is used in a lot of projects, but was consolidated in nodejs. Libuv is the responsible by the thread pool, the thread pool is used to execute more then one tasks at same time, by default, some native nodejs functions are connected with libuv, like crypto, fs and others, because it, they not execute in single thread.\n\nBy default, the thread pool has 4 threads, but we can change it to more or less with the env var: UV_THREADPOOL_SIZE\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## Multi Threading 2.0\n\nOk, but the js code we write continue being single-thread, not? Yes, but we can utilize libuv features to turn it multi thread, it is possible by nodejs cluster.\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## Callback Queue\n\nHow we saw, some tasks are sended to be executed in another thread, allowing V8 continue working with the next functions, but, all this functions that go to another thread needs a callback, a function that executes when the main functions ends. How V8 is single-thread and only one stack is available the callbacks needs to wait your turn, and in this wait time, they stay at the Callback Queue.\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## OS Delegations\n\nSome modules of NodeJS use the OS to execute the task, winning this way more threads to run our code, a example of these modules are HTTP and HTTPS. \n\nOBS: Libuv that makes this delegations.\n  \n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n## Author\n\nNathan Cotrim Lemos - MIT License\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthan-dev%2Fis-node-single-thread","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthan-dev%2Fis-node-single-thread","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthan-dev%2Fis-node-single-thread/lists"}