[Node.js] Stream all things!
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!的更多相关文章
- 9、Node.js Stream(流)
#########################################################################介绍Node.js Stream(流)Stream 是 ...
- Node.js stream 流学习
由于node.js 创建http 是这样的 http.createServer(function(request,response){}).listen(2000); 里面的request 就是rea ...
- Node.js Stream(流)
Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请求的request 对象就是一个 Stream,还有stdout(标准输出). Node.js,Str ...
- 24.Node.js Stream(流)
转自:http://www.runoob.com/nodejs/nodejs-stream.html Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请 ...
- Node.js Stream - 实战篇
邹斌 ·2016-07-22 11:04 背景 前面两篇(基础篇和进阶篇)主要介绍流的基本用法和原理,本篇从应用的角度,介绍如何使用管道进行程序设计,主要内容包括: 管道的概念 Browserify的 ...
- node.js Stream Buffer FsPromise
Stream: 类似这样:a.pipe(b).pipe(c); 我想写一个b.所以: var rs=new (require('stream').Readable)(); var ws=new (re ...
- node.js stream
stream是一个接口,流是可以从一个读取或写入数据的目标对象 ,Node 中有很多对象实现了这个接口 一.nodejs stream类型 1. Readable - 可读操作. Writable ...
- Node.js——Stream
介绍 文件流:我们一般对大一点的文件实现stream的方式进行操作 http:显然http.createServer创建过程中的IncomingMessage实现了可读流的接口,ServerRespo ...
- Node.js 中的 stream
什么是 stream Stream 借鉴自 Unix 编程哲学中的 pipe. Unix shell 命令中,管道式的操作 | 将上一个命令的输出作为下一个命令的输入.Node.js stream 中 ...
随机推荐
- codevs 1004 四子连棋
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白 ...
- hdu 1394 Minimum Inversion Number 逆序数/树状数组
Minimum Inversion Number Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- [Visual Studio] SOA服务框架搭建
1.服务框架搭建 2.服务模板创建 3.Nuget引用 4.客户端调用 任务点: 1.分析SOA 2.修改SOA架构名称以及关键字 3.使用Nuget添加引用 4.选择服务模板进行创建 5.尝试调用 ...
- mySql---or和in的效率问题(和<=、>=、between之间的关系)
写在前面: 本文是直接拿取的别人的实验数据作参考,然后对数据作分析. 参考网友的测试数据结果: 在网上一直看到的是or和in的效率没啥区别,一直也感觉是这样,前几天刚好在看<mysql数据库开发 ...
- DOM事件绑定方式
普通事件可以直接绑定 比如document.onmouseover=fn; 或者document.addEventListener("mouseover",fn,flase); a ...
- TeamViewer运行在Windows Server 2008下连接时错误提示:正在初始化显示参数
这个是使用远程桌面安装和使用Teamviewer的问题,解决方法: 实际上安装完成后TeamViewer有两个ID,一个是个人ID(就是上面卡住的780 567 914),另一个是服务器ID,我们通过 ...
- MP1593 RT8272 ACT4070 制作的DC-DC稳压电源
http://www.ideyi.org/article/11-05/2575971305526398.html?sort=2068_2073_2092_0 MP1593制作的DC-DC稳压电源,这款 ...
- Mysql 5.6 慢日志配制
一.配制my.cnf(/etc/my.cnf) slow_query_log=onlong_query_time=1slow_query_log_file=/var/log/slow_query.lo ...
- 图之Dijkstra算法
Dijkstra算法是一种求单源最短路的算法,即从一个点开始到所有其他点的最短路.其步骤如下: c语言实现如下:(使用邻接矩阵存储) #include <stdio.h> #include ...