Node.js come alone with many Stream API. Stream is useful when handling large trunck of data.

For example, we have a big file to read from file system:

// create-big-file.js

const fs = require('fs')
const file = fs.createWriteStream('./big.file') for (let i = 0; i <= 1e6; i++) {
file.write('awefawgrga afewfa')
} file.end();

If we are reading it using normal API:

const fs = require('fs')
const server = require('http').createServer();
server.on('request', (req, res) => {
fs.readFile('./big.file', (err, data) => {
if (err) throw err; res.end(data);
})
});
server.listen(8000);

When running the code, we found that the normal memory cost is 8MB, when reading the large file, memory cost is 400+MB, basiclly it buffer all the file content into the memory, this is not the way we want.

Using Stream:

const fs = require('fs')
const server = require('http').createServer();
server.on('request', (req, res) => {
/*fs.readFile('./big.file', (err, data) => {
if (err) throw err; res.end(data);
})*/
const src = fs.createReadStream('./big.file')
src.pipe(res)
}); server.listen(8000);

After updating the code, the memory usage of stream version stay around 30-40MB.

[Node.js] Stream all things!的更多相关文章

  1. 9、Node.js Stream(流)

    #########################################################################介绍Node.js Stream(流)Stream 是 ...

  2. Node.js stream 流学习

    由于node.js 创建http 是这样的 http.createServer(function(request,response){}).listen(2000); 里面的request 就是rea ...

  3. Node.js Stream(流)

    Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请求的request 对象就是一个 Stream,还有stdout(标准输出). Node.js,Str ...

  4. 24.Node.js Stream(流)

    转自:http://www.runoob.com/nodejs/nodejs-stream.html Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请 ...

  5. Node.js Stream - 实战篇

    邹斌 ·2016-07-22 11:04 背景 前面两篇(基础篇和进阶篇)主要介绍流的基本用法和原理,本篇从应用的角度,介绍如何使用管道进行程序设计,主要内容包括: 管道的概念 Browserify的 ...

  6. node.js Stream Buffer FsPromise

    Stream: 类似这样:a.pipe(b).pipe(c); 我想写一个b.所以: var rs=new (require('stream').Readable)(); var ws=new (re ...

  7. node.js stream

    stream是一个接口,流是可以从一个读取或写入数据的目标对象 ,Node 中有很多对象实现了这个接口   一.nodejs stream类型 1. Readable - 可读操作. Writable ...

  8. Node.js——Stream

    介绍 文件流:我们一般对大一点的文件实现stream的方式进行操作 http:显然http.createServer创建过程中的IncomingMessage实现了可读流的接口,ServerRespo ...

  9. Node.js 中的 stream

    什么是 stream Stream 借鉴自 Unix 编程哲学中的 pipe. Unix shell 命令中,管道式的操作 | 将上一个命令的输出作为下一个命令的输入.Node.js stream 中 ...

随机推荐

  1. 【DFS好题】BZOJ1999- [Noip2007]Core树网的核(数据加强版)

    NOIP的数据好水,一开始有好几个错结果NOIP数据就水过了?? [题目大意] 求无根树的直径上一段不超过S长的链,使得偏心距最小.具体概念见原题. [思路] 首先明确几个性质: (1)对于树中的任意 ...

  2. NOIP2018 RP++

    飞吧,不用看向地面. NOIP,RP++.

  3. python开发_webbrowser_浏览器控制模块

    ''' python的webbrowser模块支持对浏览器进行一些操作 主要有以下三个方法: webbrowser.open(url, new=0, autoraise=True) webbrowse ...

  4. Python开发_python的安装

    Python几乎可以在任何平台下运行,如我们所熟悉的:Windows/Unix/Linux/Macintosh. 在这里我们说一下,在Windows操作系统中安装python. 我的操作系统为:Win ...

  5. Codeforces Round #293 (Div. 2) C. Anya and Smartphone 数学题

    C. Anya and Smartphone time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. ROS知识(16)----如何编译时自动链接同一个工作空间的其他包的头文件(包含message,srv,action自动生成的头文件)

    catkin_make编译时,往往需要自动链接同一个工作空间的其他包的头文件.否则会出现类似如下的错误: /home/xx/xx_ws/srcA_package/src/db.hpp:13:26: f ...

  7. Mac下使用ABTestingGateway快速搭建灰度网关

    ABTestingGateway简介 ABTestingGateway 是新浪开源的一个可以动态设置分流策略的灰度发布系统,工作在7层,基于nginx和ngx-lua开发,使用 redis 作为分流策 ...

  8. PHPExcel 导出2003和2007的excel文档实例

    require_once 'common/excel/PHPExcel.php'; require_once 'common/excel/phpExcel/Writer/Excel2007.php'; ...

  9. SmartProg2 Universal, ISP capable programmer

    http://www.elnec.com/products/universal-programmers/smartprog2/ 40 powerful TTL pindrivers provide H ...

  10. LINUX mysql 源码安装

    一.下载编译安装 #cd /usr/local/src/ #wget http://mysql.byungsoo.net/Downloads/MySQL-5.1/mysql-5.1.38.tar.gz ...