Why Nodejs serves a file with 80x more CPU usage than Nginx?

综合技术 2018-02-14

Take the same code that sits on nodejs.org home page. Serve a static file that is 1.8Mb. And do the same with Nginx, and watch the difference.

Code :

Screencast :

Please share if you know anything that'd prevent this from happening, so we don't need to deploy nginx servers and complicate our lives.

ps1. this test is done with node 0.6.12. out of curiosity, i downgraded to 0.4.12 just to check if it's a regression, on the contrary, it was worse. same file used 25% twice.

ps2. this post is not a nodejs hate - we use nodejs, and we love it, except this glitch which actually delayed our launch (made us really sad), and seemed quite serious to me. i've never read, heard, seen or expected to come across.

Problem courtesy of: Devrim


The problem with your node benchmark is that you store the static file in a variable inside the V8 heap. Due to the way how V8 handles memory it can't directly send data contained in javascript variables to the network, because addresses of allocated objects may change during runtime, therefore V8 has to make a copy of your 1.8MB string on every request, sure that kills performance.

What you could do is to use a Buffer

replace: longAssString = fs.readFileSync(pathToABigFile, 'utf8');

with: longAssString = fs.readFileSync(pathToABigFile);

that way you have your static file in a buffer, buffers are stored outside of V8s heap and require no copy when sent to the network and should therefore be much faster.

Solution courtesy of: stewe


基于hi-nginx的web开发(python篇)——动态路由和请求方法... hi.py 的提供的路由装饰器接受两个参数,第一个参数指定动态路由的正则模式,第二个参数指定同意的http请求方法列表。 比如: 1 @app.route(r"^/client/?$",) 2 def client(req,res,param): 3 res.cont...
Adding SSL and a domain name to Jupyter Hub This is the fifth part of a multi-part series that shows how to set up Jupyter Hub for a college class. In this post, we are going to link a domai...
NODEJS–HTTP实现分析 1.前言 目前,HTTP协议是互联网上应用最为广泛的一种网络协议,也是前端er接触最多的一种协议。通过阅读http模块在nodejs中的实现,能够更深入的了解HTTP协议。HTTP协议是基于TCP协议之上的应用层协议,它的实现离不开TCP/IP协议族。而具体到代码实现,http模块依...
How to Use Wikipedia API With Node.js Wikipedia has an enormous data set such as page view statistics, tables, etc. In this article, I will show you how to access the statist...
NGINX Named an “IDC Innovator” in Cloud-Native App... NGINX earns prestigious industry recognition as the cloud‑native ADC powering most of the world’s busiest websites and applications. We are...
Node.js Recipes

责编内容来自:Node.js Recipes (本文源链)
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。