node.js - Why blocking code behaves better than non-blocking? -


i have knocked 2 little node.js snippets, doing same thing, written in blocking , non-blocking ways, measure performance diference. here goes:

non-blocking (the traditional node.js way):

var http = require('http'); var fs = require('fs');  var app = http.createserver(function(req, res){   fs.readfile('lorem.txt',function(err, data){     res.end(data);   }); });  app.listen(8080); 

blocking:

var http = require('http'); var fs = require('fs');  var app = http.createserver(function (req, res) {   res.end(fs.readfilesync('lorem.txt')); });  app.listen(8080); 

lorem.txt text file 33kb in size.

running apache benchmark on against both shows no difference, or better performance blocking version.

ab -n 100 -c 10 http://locahost:8080/

blocking: time per request: 5.701 ms
non-blocking: time per request: 8.401 ms

asynchronous method have better performance in broader sense i.e. smaller response times more (majority) concurrent requests.

the benchmark did corner case. suppose increase number of concurrent requests or increase size of response returned. sure synchronous method perform miserably. synchronous method not because of smaller response bad because not scalable @ all. average modern day computer can handle read/writes @ such scale. if increase filesze 1mb ? or requests 1 million? think server has deal gigabytes of storage , serve millions of requests.

the core concept of node.js i.e. asynchronous, non-blocking, event driven i/o built solve problems of scalability c10k. conclusion come one-sided, sure asynchronous method win 99% of time in real world.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -