[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 中 ...
随机推荐
- 【字符串哈希】The 16th UESTC Programming Contest Preliminary F - Zero One Problem
题意:给你一个零一矩阵,q次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...
- bzoj 1563
对于很多决策单调性DP问题,我们很难(但不是不可以)证明其决策满足单调性,所以感觉很像时,可以打表看是否满足. 这道题的精度(?范围)很难搞,开始生怕溢出,看了hzwer的代码,才发现用long do ...
- bzoj 3611
和BZOJ消耗站一样,先将那个询问的简图构建出来,然后就是简单的树形DP. (倍增数组开小了,然后就狂WA,自己生成的极限数据深度又没有那么高,链又奇迹般正确) #include <cstdio ...
- 更好的浏览器动画实现 requestAnimationFrame
requestAnimationFrame 是专门为实现高性能的帧动画而设计的一个API: js一般是借助setTimeout或setInterval这两个函数实现动画,性能不佳. css3动画,性能 ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块
C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...
- 自动打怪 c#
其中思路很简单,单线程的一个乱七八糟的游戏 预计会更新背包,背包这个估计会用一个vector来存 图形的话,我得催催我的美工大人,她会帮我弄吧,哇哈哈 界面: namespace auttompk { ...
- Linux下生成随机密码(转)
1.使用SHA算法来加密日期,并输出结果的前32个字符: date +%s |sha256sum |base64 |head -c 32 ;echo 生成结果如下: ZTNiMGM0NDI5OGZjM ...
- 2013Esri全球用户大会之ArcGIS for Server&Portal for ArcGIS
Q1:ArcGIS 10.2 for Server有哪些新特性? ArcGIS 10.2对于ArcGIS for Server来说是一个引人注目的版本.它建立在ArcGIS 10.1扎实雄厚的基础上, ...
- SQLite 客户端管理工具
SQLite 客户端管理工具 SQLite Expert Personal 3.5.79.2499 下载地址:http://www.onlinedown.net/soft/117987.htm SQL ...
- 用Qemu模拟vexpress-a9 (六) --- 多核
环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 32 u-boot 版本:u-boot-2015-04 Linux kernel版本:linux-3.16.y busyb ...