Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

胜者是 Stream:NodeJS 读取大数据集合几种方法的性能比较 #6264

Merged
Changes from 1 commit
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
bf2f179
Merge pull request #1 from xitu/master
LucaslEliane Mar 9, 2019
52da98e
ci: Merge remote-tracking branch 'upstream/master'
Mar 14, 2019
6ce5b40
ci: Merge remote-tracking branch 'upstream/master'
Mar 27, 2019
df2af68
ci: Merge remote-tracking branch 'upstream/master'
Apr 16, 2019
a2dc4eb
ci: Merge remote-tracking branch 'upstream/master'
LucaslEliane May 22, 2019
a5794b3
ci: Merge remote-tracking branch 'upstream/master'
May 26, 2019
9d5a366
translation/effiective BLoC pattern for flutter
May 26, 2019
816153d
ci: Merge remote-tracking branch 'upstream/master'
LucaslEliane Jun 24, 2019
2fdee6d
feat: merge upstream master
Jul 7, 2019
6dde351
feat: merge upstream master
Jul 7, 2019
664b0de
ci: Merge branch 'master' of github.com:LucaslEliane/gold-miner
Jul 7, 2019
7276e78
微前端:未来前端开发的新趋势 — 第二部分 (#6068)
lihaobhsfer Jul 7, 2019
1911ad1
Create kotlin-clean-architecture.md (#6085)
leviding Jul 7, 2019
52991c9
Create the-css-mindset.md (#6088)
sunui Jul 8, 2019
58c4a94
Xcode 和 LLDB 高级调试教程:第 2 部分 (#6056)
kirinzer Jul 8, 2019
893ea6b
Create collection-cognitive-biases-how-to-use-1.md (#6092)
leviding Jul 8, 2019
af3dbe2
Create collection-cognitive-biases-how-to-use-2.md (#6094)
leviding Jul 8, 2019
dc7bada
Create collection-cognitive-biases-how-to-use-3.md (#6093)
leviding Jul 8, 2019
dffe380
微前端:未来前端开发的新趋势 — 第四部分 (#6058)
Usey95 Jul 8, 2019
f71e0e4
Create the-state-of-graphql-by-reddit.md (#6099)
sunui Jul 9, 2019
5033f8c
Create birdseye-go.md (#6100)
sunui Jul 9, 2019
0013770
Create fountaincodes.md (#6101)
sunui Jul 9, 2019
ed91fa6
Create testing-react-apps-with-cypress.md (#6102)
sunui Jul 9, 2019
7cc5073
Update birdseye-go.md
sunui Jul 9, 2019
e26b29b
自托管你的静态资源 (#6060)
twang1727 Jul 9, 2019
a962c87
微前端:未来前端开发的新趋势 — 第三部分 (#6067)
xilihuasi Jul 10, 2019
3e3da1e
Web 流式文字排版的现状 (#6071)
Jenniferyingni Jul 10, 2019
0bece03
纠错 (#6112)
lvsheng Jul 11, 2019
c1f9290
纠错 (#6111)
lvsheng Jul 11, 2019
b72444e
推广 PWA 安装的模式(移动端) (#6087)
xutaogit Jul 11, 2019
4ee337b
使用 SVG 和 Vue.Js 构建动态树图 (#6075)
YueYongDev Jul 12, 2019
8a8a02b
在 Python 中过度使用列表推导式和表达式生成器 (#6059)
ccJia Jul 12, 2019
ee24c01
设计任何图表的六项原则 (#6083)
MarchYuanx Jul 12, 2019
2c0958a
Create creating-a-multi-level-hierarchical-flyout-navigation-menu-usi…
sunui Jul 12, 2019
5fa8ae4
Create css-architecture-for-multiple-websites.md (#6119)
sunui Jul 12, 2019
14f54b9
Update css-architecture-for-multiple-websites.md
sunui Jul 12, 2019
3ef25b9
Create how-pagespeed-works.md (#6121)
sunui Jul 12, 2019
6154f0d
CSS 思维模式 (#6124)
MarchYuanx Jul 13, 2019
e6ed3c3
使用 Gomobile 和 Gopherjs 的动态二维码数据传输 (#6096)
EmilyQiRabbit Jul 13, 2019
1c67d83
使用 Swift 5 构建一个 iOS 移动端群聊应用程序 (#6084)
LucaslEliane Jul 13, 2019
1217bb4
Create the-10-statistical-techniques-data-scientists-need-to-master.m…
leviding Jul 13, 2019
bb1be4e
Create 101-tips-for-being-a-great-programmer-human.md (#6128)
leviding Jul 13, 2019
6279ab4
Create xgboost-algorithm-long-may-she-reign.md (#6127)
leviding Jul 13, 2019
ef1f063
Update the-10-statistical-techniques-data-scientists-need-to-master.md
leviding Jul 13, 2019
eb4cb43
Create a-step-by-step-explanation-of-principal-component-analysis.md …
leviding Jul 13, 2019
00a04c5
Create when-to-standardize-your-data.md (#6130)
leviding Jul 13, 2019
bec61ea
利用 84 种认知偏见设计更好的产品 —— 第一部分 (#6114)
JalanJiang Jul 14, 2019
3d8e2fb
Create responsive-design-ground-rules.md (#6141)
sunui Jul 15, 2019
3273e43
Create 16-devtools-tips-and-tricks-every-css-developer-need-to-know.m…
lsvih Jul 15, 2019
5144968
Create hermes.md (#6145)
leviding Jul 15, 2019
1ba28c1
敏捷也许是个问题 (#6105)
Charlo-O Jul 16, 2019
703437d
Create frontend-vs-backend-which-one-is-right-for-you.md (#6148)
sunui Jul 16, 2019
aa39969
Create javascript-knowledge-reading-source-code.md
leviding Jul 16, 2019
d03b704
Update javascript-knowledge-reading-source-code.md
leviding Jul 16, 2019
c78e051
npm 的经济风云 —— 下半部分 (#6098)
JaxNext Jul 16, 2019
37cdaa3
Git:透过命令学概念 —— 第二部分 (#6113)
Mirosalva Jul 16, 2019
0615faf
区域设置更改和 AndroidViewModel 反面模式 (#6080)
solerji Jul 16, 2019
647378d
更新七月份杂项积分和礼物兑换积分
leviding Jul 16, 2019
5c7d92c
fix 积分
leviding Jul 16, 2019
203ba75
更新七月份部分前端分类文章翻译校对积分
leviding Jul 16, 2019
8daa230
更新七月份前端分类文章翻译校对积分
leviding Jul 16, 2019
d33f26e
Update front-end.md
leviding Jul 16, 2019
a8b10c0
Update backend.md
leviding Jul 16, 2019
a046264
fix 积分
leviding Jul 16, 2019
75d8c0e
Update ios.md
leviding Jul 16, 2019
2d619b6
Update ios.md
leviding Jul 16, 2019
5a050b8
Update AI.md
leviding Jul 16, 2019
bea3338
Update android.md
leviding Jul 16, 2019
b9e838f
Update product.md
leviding Jul 16, 2019
586b62c
Update design.md
leviding Jul 16, 2019
0ff7fad
更新七月上半月其他文章翻译校对积分
leviding Jul 16, 2019
4aa4854
fix 积分
leviding Jul 16, 2019
54a9181
ci: Merge remote-tracking branch 'upstream/master'
LucaslEliane Jul 17, 2019
fe49e75
NodeJS 读取大数据集合的几种方法的性能比较
LucaslEliane Aug 1, 2019
2b5d732
Update effective-bloc-pattern.md
leviding Aug 1, 2019
1bf3fae
Update streams-for-the-win-a-performance-comparison-of-nodejs-methods…
LucaslEliane Aug 8, 2019
9001a9e
Update streams-for-the-win-a-performance-comparison-of-nodejs-methods…
LucaslEliane Aug 16, 2019
25b8e81
为代码块添加标签
lsvih Aug 25, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner)
> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO1/streams-for-the-win-a-performance-comparison-of-nodejs-methods-for-reading-large-datasets-pt-2.md](https://github.com/xitu/gold-miner/blob/master/TODO1/streams-for-the-win-a-performance-comparison-of-nodejs-methods-for-reading-large-datasets-pt-2.md)
> * 译者:[lucasleliane](https://github.com/LucaslEliane)
> * 校对者:
> * 校对者:[Ultrasteve](https://github.com/Ultrasteve),[xilihuasi](https://github.com/xilihuasi)

# 胜者是 Stream:NodeJS 读取大数据集合几种方法的性能比较

## readFile()、createReadStream() 以及事件流如何相互比较

![](https://cdn-images-1.medium.com/max/2000/1*fsseXIPGEhwmg6kfgXyIjA.jpeg)

如果你最近都在阅读我的文章,你应该会看到我几周前发布的一篇[博客](https://itnext.io/using-node-js-to-read-really-really-large-files-pt-1-d2057fe76b33),这篇博客讨论了使用 Node.js 来读取大型数据集的各种方法。
如果你一直在关注我的文章,你应该会看到我几周前发布的一篇[博客](https://juejin.im/post/5d4b99eb5188257bc15d299f),这篇博客讨论了使用 Node.js 来读取大型数据集的各种方法。

令我惊讶的是,这篇博客受到了很多读者的喜爱 - 这个主题(对于我来说)似乎在很多其他的帖子、博客或者论坛上已经讨论过了,但是无论如何,它都吸引了很多人的关注。所以,感谢所有花时间来阅读这篇博客的读者!对此,我真的非常感激。

Expand All @@ -21,7 +21,7 @@

### 上一篇文章中的挑战

我不会详细介绍上一篇博客中的挑战和解决方案,因为你可以去阅读我的第一篇文章,了解所有的细节[这里](https://itnext.io/using-node-js-to-read-really-really-large-files-pt-1-d2057fe76b33),但是我会给你进行一下简单的介绍。
我不会详细介绍上一篇博客中的挑战和解决方案,因为你可以去阅读我的第一篇文章,[这里](https://juejin.im/post/5d4b99eb5188257bc15d299f)可以了解所有细节,但是我会给你进行一下简单的介绍。

这个挑战是来自于 Slack 频道的人发布的一个编码挑战,要求读取一个非常大的数据集(总共大小超过 2.5 GB),解析数据并且提取各种信息。

Expand All @@ -34,9 +34,9 @@

这里是数据链接:​[https://www.fec.gov/files/bulk-downloads/2018/indiv18.zip](https://www.fec.gov/files/bulk-downloads/2018/indiv18.zip)

#### 三种解决方案与小数据集情况下的不同点
#### 三种小数据集场景下的解决方案

当我努力去实现处理大型数据集这个目标的过程中,我在 Node.js 中提出了三个解决方案
当我努力去实现处理大型数据集这个目标的过程中,我在 Node.js 中想到了三个解决方案

**解决方案 1:[`fs.readFile()`](https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback)**

Expand All @@ -48,7 +48,7 @@

**解决方案 3:[`event-stream`](https://www.npmjs.com/package/event-stream)**

最后,我在 Node.js 中提出了唯一能够处理完整的 2.55 GB 的文件的解决方案。
最后,我在想到了 Node.js 中唯一能够处理完整的 2.55 GB 的文件的解决方案。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我在想到了 => 我想到了在

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改 感谢


> 有趣的是:Node.js 在任何时候,都只能够在内存中容纳 1.67 GB,之后就会抛出 JavaScript 的 `heap out of memory` 错误。

Expand Down Expand Up @@ -83,7 +83,7 @@ console.timeEnd('label1');
// 输出的结果类似于这样: label1 0.002ms
```

这是我用来计算处理数据集所需要的时间的一种方法
这是我用来计算处理数据集所需时间的一种方法

[**`performance-now`**](https://www.npmjs.com/package/performance-now)

Expand Down Expand Up @@ -114,13 +114,13 @@ console.log('Performance for timing for label:' + (end — start).toFixed(3) + '

我想同时使用 Node 的 `console.time()` 和 `performance-now`,我可以规避差异并且获得关于文件解析函数真正的执行时间的准确值。

下面是我在每个脚本中接入 `console.time()` 以及 `performance-now` 的代码片段。这些代码只是每个函数的片段 - 相对于完整代码来说,你可以在[这里](https://github.com/paigen11/file-read-challenge)查看我的代码仓库。
下面是我在每个脚本中接入 `console.time()` 以及 `performance-now` 的代码片段。这些代码只是每个函数的片段 - 如果你想看全部代码,你可以在[这里](https://github.com/paigen11/file-read-challenge)查看我的代码仓库。

**fs.readFile() 代码实现示例**

![](https://cdn-images-1.medium.com/max/2568/1*n48UZ77lvktwjN6IDR0x1g.png)

由于这个脚本使用 `fs.readFile()` 实现,整个文件都会在执行函数之前被读取到内存中,看起来这是最同步的代码。但是它实际上不是同步的,同步地读取文件在 Node 中有一个专用的方法,叫做 `fs.readFileSync()`,两者看起来很相似。
由于这个脚本使用 `fs.readFile()` 实现,整个文件都会在执行函数之前被读取到内存中,这看起来很像同步代码。但是它实际上不是同步的,同步地读取文件在 Node 中有一个专用的方法,叫做 `fs.readFileSync()`,两者看起来很相似。

但是,我们很容易看到文件的总行数以及两个计时器方法,来确定执行行计数到底花费了多长时间。

Expand Down Expand Up @@ -188,7 +188,7 @@ console.log('Performance for timing for label:' + (end — start).toFixed(3) + '

#### 结论

最后,Node.js 原生的流和非原生的流,在处理大型数据集的时候会更加有效
最后,流式处理,不论是 Node.js 原生的亦或是非原生的,在处理大型数据集的时候都会更加有效

感谢你继续阅读了本系列文章的第二部分。如果你想要再次阅读第一篇文章,可以看[这里](https://itnext.io/using-node-js-to-read-really-really-large-files-pt-1-d2057fe76b33)。

Expand Down