[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 中 ...
随机推荐
- Mac OS下不产生.DS_Store 隐藏文件和清理.DS_Store的方法
一.清理.DS_Store的方法 1. 打开终端 (Macintosh HD > Applications > Utilities > Terminal)2. 输入命令: " ...
- Windows Server 2008 R2的web服务器nginx和Apache的比较
因为很喜欢nginx,所以也想尝试在Windows下使用nginx,前面安装配置都挺顺利,把域名解析尽量后,通过域名代理访问jboss,却异常的慢,起码有3秒的时间才显示页面,而这个页面是jboss的 ...
- 【JavaScript代码实现一】数组去重
function arrayNoDupulate(array) { var hash = {}; var result = []; for(var i=0;i<array.length;i++) ...
- Elasticsearch快速入门案例
写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths 构造
D. Bear and Two Paths 题目连接: http://www.codeforces.com/contest/673/problem/D Description Bearland has ...
- mysql反向解析导致连接缓慢
Content 0.序 1.问题 2.原因 3.解决办法 0.序 本文主要是记录Mysql安装在 VMWARE下,本地连接Mysql速度很慢的原因及解决办法. 1.问题 本地的一个网站使用mysql数 ...
- 你的C/C++程序为什么无法运行?揭秘Segmentation fault (2)
什么让你对C/C++如此恐惧? 本篇将继续上一篇来讨论段错误(Segmentation fault). 上一篇: 你的C/C++程序为什么无法运行?揭秘Segmentation fault(1) 追溯 ...
- 华为S5300系列升级固件S5300SI-V100R005C01SPC100.cc
这个固件附带了web,注意,这个插件是升级V200的必经固件,所以必须升级为此固件之后才能往下升级. 升级小插曲: 1.升级的使用使用Windows,不要用Mac或者Linux,因为从Mac/Linu ...
- CentOS使用chkconfig增加开机服务提示service xxx does not support chkconfig的问题解决
在shell文件的第二行增加如下内容即可: # chkconfig: 2345 10 90 #服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10. # descrip ...
- mysql学习心得转
http://www.cnblogs.com/lyhabc/p/3691555.html