├── .babelrc.json ├── .eslintrc.cjs ├── .gitignore ├── .husky ├── .gitignore └── pre-commit ├── LICENSE ├── README.md ├── benchmark.sh ├── package-lock.json ├── package.json └── src ├── http-server-classic.js ├── http-server-iterator-await.js └── http-server-iterator-no-await.js /.babelrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "@babel/plugin-syntax-top-level-await" 4 | ] 5 | } -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | es2021: true, 4 | node: true 5 | }, 6 | extends: [ 7 | 'standard' 8 | ], 9 | parser: '@babel/eslint-parser', 10 | parserOptions: { 11 | ecmaVersion: 12, 12 | sourceType: 'module' 13 | }, 14 | rules: { 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | -------------------------------------------------------------------------------- /.husky/.gitignore: -------------------------------------------------------------------------------- 1 | _ 2 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . "$(dirname "$0")/_/husky.sh" 3 | 4 | npx lint-staged 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Luciano Mammino 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # async-iteration-http-requests 2 | 3 | Playing around with async iterators with HTTP servers. 4 | 5 | ## Rationale 6 | 7 | You can use `for await...of` to handle HTTP requests using the Node.js HTTP server: 8 | 9 | ```javascript 10 | import { createServer } from 'http' 11 | import { on } from 'events' 12 | 13 | const server = createServer() 14 | server.listen(8000, console.log) 15 | 16 | for await (const [req, res] of on(server, 'request')) { 17 | console.log(req.url) 18 | res.end('hello') 19 | } 20 | ``` 21 | 22 | This is an interesting pattern that has been popularised by the likes of Deno and you can easily achieve the same behaviour also in Node.js. 23 | 24 | The problem is that, if you use `await` inside the `for await...of` loop, you essentially end up handling all the incoming requests in series (a.k.a. no concurrency!) and this can severely affect the performance of you server. 25 | 26 | For example: 27 | 28 | ```javascript 29 | import { createServer } from 'http' 30 | import { on } from 'events' 31 | import { setTimeout } from 'timers/promises' 32 | 33 | const server = createServer() 34 | server.listen(8000, console.log) 35 | 36 | for await (const [, res] of on(server, 'request')) { 37 | await setTimeout(1000) 38 | res.end('hello') 39 | } 40 | ``` 41 | 42 | This code is a BAD IDEA for the use case of HTTP servers! 43 | 44 | You should not await inside a `for await...of` if you want concurrency! 45 | 46 | A better approach would be: 47 | 48 | ```javascript 49 | import { createServer } from 'http' 50 | import { on } from 'events' 51 | import { setTimeout } from 'timers/promises' 52 | 53 | async function handleRequest (req, res) { 54 | await setTimeout(1000) 55 | res.end('hello') 56 | } 57 | 58 | const server = createServer() 59 | server.listen(8000, console.log) 60 | 61 | for await (const [req, res] of on(server, 'request')) { 62 | handleRequest(req, res) // NOTE: no await! 63 | } 64 | ``` 65 | 66 | Which delegates `handleRequest` to deal with the request but it does not await it's result! 67 | 68 | This repository provides a set of examples and a benchmark script to validate what's stated above. 69 | 70 | More details are available on Twitter: 71 | 72 | - [Original Tweet](https://twitter.com/loige/status/1388879048617590790) 73 | - [Second tweet with more details](https://twitter.com/loige/status/1388888755495387137) 74 | 75 | Make sure to check out all the awesome comments! 76 | 77 | ## Install 78 | 79 | You will need Node.js 16+. 80 | 81 | Then you'll need to install the needed dependencies with: 82 | 83 | ```bash 84 | npm install 85 | ``` 86 | 87 | ## Code examples: 88 | 89 | They are all in the [`src`](/src) folder. 90 | 91 | - `http-server-classic.js`: it's a simple server that does not use `for await...of` 92 | - `http-server-iterator-await.js`: uses `for await...of` and uses `await` inside the loop, therefore exposing the performance degradation discussed above. 93 | - `http-server-iterator-no-await.js`: uses `for await...of` but does not await inside the loop. In this case performances are not degraded. 94 | 95 | 96 | ## Run the benchmark 97 | 98 | On linux or macOs run `./benchmark.sh` or `npm run benchmark`. 99 | 100 | ## Contributing 101 | 102 | Everyone is very welcome to contribute to this project. 103 | You can contribute just by submitting bugs or suggesting improvements by 104 | [opening an issue on GitHub](https://github.com/lmammino/async-iteration-http-requests/issues). 105 | 106 | ## License 107 | 108 | Licensed under [MIT License](LICENSE). © Luciano Mammino. 109 | 110 | 111 | ## Shameless plug 😇 112 | 113 | 114 | 115 | If you like this piece of work, consider supporting me by getting a copy of [Node.js Design Patterns, Third Edition](https://www.nodejsdesignpatterns.com/), which also goes into great depth about Streams and related design patterns. 116 | 117 | If you already have this book, **please consider writing a review** on Amazon, Packt, GoodReads or in any other review channel that you generally use. That would support us greatly 🙏. 118 | -------------------------------------------------------------------------------- /benchmark.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | echo "Benchmark http-server-classic.js" 6 | echo "--------------------------------" 7 | node src/http-server-classic.js & 8 | SERVER_PID=$! 9 | node_modules/.bin/autocannon http://localhost:8000 10 | kill $SERVER_PID 11 | 12 | sleep 1 13 | 14 | echo "" 15 | echo "Benchmark http-server-iterator-await.js" 16 | echo "---------------------------------------" 17 | node src/http-server-iterator-await.js & 18 | SERVER_PID=$! 19 | node_modules/.bin/autocannon http://localhost:8000 20 | kill $SERVER_PID 21 | 22 | sleep 1 23 | 24 | echo "" 25 | echo "Benchmark http-server-iterator-no-await.js" 26 | echo "------------------------------------------" 27 | node src/http-server-iterator-no-await.js & 28 | SERVER_PID=$! 29 | node_modules/.bin/autocannon http://localhost:8000 30 | kill $SERVER_PID 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "http-test", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "http-server.js", 6 | "type": "module", 7 | "engines": { 8 | "node": ">=16.0.0" 9 | }, 10 | "engineStrict": true, 11 | "scripts": { 12 | "test": "eslint .", 13 | "benchmark": "./benchmark.sh", 14 | "prepare": "husky install" 15 | }, 16 | "keywords": [], 17 | "author": "Luciano Mammino", 18 | "license": "MIT", 19 | "devDependencies": { 20 | "@babel/core": "^7.14.0", 21 | "@babel/eslint-parser": "^7.13.14", 22 | "@babel/plugin-syntax-top-level-await": "^7.12.13", 23 | "autocannon": "^7.2.0", 24 | "eslint": "^7.25.0", 25 | "eslint-config-standard": "^16.0.2", 26 | "eslint-plugin-import": "^2.22.1", 27 | "eslint-plugin-node": "^11.1.0", 28 | "eslint-plugin-promise": "^4.3.1", 29 | "husky": "^6.0.0", 30 | "lint-staged": "^10.5.4" 31 | }, 32 | "lint-staged": { 33 | "*.js": "eslint --cache --fix" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/http-server-classic.js: -------------------------------------------------------------------------------- 1 | import { createServer } from 'http' 2 | import { setTimeout } from 'timers/promises' 3 | 4 | const server = createServer(async function (req, res) { 5 | await setTimeout(1000) 6 | res.end('hello') 7 | }) 8 | 9 | server.listen(8000, console.log) 10 | -------------------------------------------------------------------------------- /src/http-server-iterator-await.js: -------------------------------------------------------------------------------- 1 | import { createServer } from 'http' 2 | import { on } from 'events' 3 | import { setTimeout } from 'timers/promises' 4 | 5 | const server = createServer() 6 | server.listen(8000, console.log) 7 | 8 | for await (const [, res] of on(server, 'request')) { 9 | await setTimeout(1000) 10 | res.end('hello') 11 | } 12 | -------------------------------------------------------------------------------- /src/http-server-iterator-no-await.js: -------------------------------------------------------------------------------- 1 | import { createServer } from 'http' 2 | import { on } from 'events' 3 | import { setTimeout } from 'timers/promises' 4 | 5 | async function handleRequest (req, res) { 6 | await setTimeout(1000) 7 | res.end('hello') 8 | } 9 | 10 | const server = createServer() 11 | server.listen(8000, console.log) 12 | 13 | for await (const [req, res] of on(server, 'request')) { 14 | handleRequest(req, res) // NOTE: no await! 15 | } 16 | --------------------------------------------------------------------------------