[RxJS] Stream Processing With RxJS vs Array Higher-Order Functions
Higher order Array functions such as filter, map and reduce are great for functional programming, but they can incur performance problems.
var ary = [1,2,3,4,5,6]; var res = ary.filter(function(x, i, arr){
console.log("filter: " + x);
console.log("create new array: " + (arr === ary));
return x%2==0;
})
.map(function(x, i, arr){
console.log("map: " + x);
return x+"!";
})
.reduce(function(r, x, i, arr){
console.log("reduce: " + x);
return r+x;
}); console.log(res); /*
"filter: 1"
"create new array: true"
"filter: 2"
"create new array: true"
"filter: 3"
"create new array: true"
"filter: 4"
"create new array: true"
"filter: 5"
"create new array: true"
"filter: 6"
"create new array: true"
"map: 2"
"map: 4"
"map: 6"
"reduce: 4!"
"reduce: 6!"
"2!4!6!"
*/
In the example, filter & map function will return a new array. That's good because it pushes forward the idea of immutability. However, it's bad because that means I'm allocating a new array. I'm iterating over it only once, and then I've got to garbage-collect it later. This could get really expensive if you're dealing with very large source arrays or you're doing this quite often.
Using RxJS:
var source = Rx.Observable.fromArray([1,2,3,4,5,6]); source.filter(function(x){
console.log("filter: " + x);
return x%2==0;
})
.map(function(x){
console.log("map: " + x);
return x+"!";
})
.reduce(function(r, x){
console.log("reduce: " + x);
return r+x;
}).subscribe(function(res){
console.log(res);
});
/*
"filter: 1"
"filter: 2"
"map: 2"
"filter: 3"
"filter: 4"
"map: 4"
"reduce: 4!"
"filter: 5"
"filter: 6"
"map: 6"
"reduce: 6!"
"2!4!6!"
*/
The biggest thing is that now you'll see it goes through each -- the filter, the map, and the reduce -- at each step.
Differences:
The first example: it creates two intermediary arrays (during filter and map). Those arrays needed to be iterated over each time, and now they'll also have to be garbage-collected.
The RxJS example: it takes every item all the way through to the end without creating any intermediary arrays.
[RxJS] Stream Processing With RxJS vs Array Higher-Order Functions的更多相关文章
- [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: Higher Order Functions
[CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: ...
- Storm(2) - Log Stream Processing
Introduction This chapter will present an implementation recipe for an enterprise log storage and a ...
- Stream Processing 101: From SQL to Streaming SQL in 10 Minutes
转自:https://wso2.com/library/articles/2018/02/stream-processing-101-from-sql-to-streaming-sql-in-ten- ...
- Apache Samza - Reliable Stream Processing atop Apache Kafka and Hadoop YARN
http://engineering.linkedin.com/data-streams/apache-samza-linkedins-real-time-stream-processing-fram ...
- Akka(23): Stream:自定义流构件功能-Custom defined stream processing stages
从总体上看:akka-stream是由数据源头Source,流通节点Flow和数据流终点Sink三个框架性的流构件(stream components)组成的.这其中:Source和Sink是stre ...
- 腾讯大数据平台Oceanus: A one-stop platform for real time stream processing powered by Apache Flink
January 25, 2019Use Cases, Apache Flink The Big Data Team at Tencent In recent years, the increa ...
- Stream processing with Apache Flink and Minio
转自:https://blog.minio.io/stream-processing-with-apache-flink-and-minio-10da85590787 Modern technolog ...
- 13 Stream Processing Patterns for building Streaming and Realtime Applications
原文:https://iwringer.wordpress.com/2015/08/03/patterns-for-streaming-realtime-analytics/ Introduction ...
- 1.2 Use Cases中 Stream Processing官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Stream Processing 流处理 Many users of Kafka ...
随机推荐
- 我的接口框架---框架函数文件common.php
<?php defined('JDHU') OR die('no allow access'); /** * 加载配置文件 */ function &get_config($replac ...
- adodb配置与使用
=========================================php100:80:ADODB PHP数据库万能引擎类 ADODB PHP数据库介绍与特点 ADODB 是一种兼容的各 ...
- mysql备份,还原命令
mysql导出数据1.导出整个数据库mysqldump -u用户名 -p 数据库名 >备份文件2.导出一个表mysqldump -u用户名 -p databaseName tablename & ...
- 读书笔记-《Training Products of Experts by Minimizing Contrastive Divergence》
Training Products of Experts by Minimizing Contrastive Divergence(以下简称 PoE)是 DBN 和深度学习理论的 肇始之篇,最近在爬梳 ...
- iOS: 学习笔记, Swift与Objective-C混用总结
Swift与Objective-C交互总结 在Swift中使用Objective-C(简单) 在创建OjbC文件时, XCode会提示创建XXX-Bridging-Header.h文件, 创建之 在创 ...
- bzoj 1486: [HNOI2009]最小圈 dfs求负环
1486: [HNOI2009]最小圈 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1022 Solved: 487[Submit][Status] ...
- linux RWT
http://www.cnblogs.com/qlwy/archive/2011/06/26/2121919.html#undefined
- Linux Shell编程(29)——函数
和"真正的"编程语言一样, Bash也有函数,虽然在某些实现方面稍有些限制. 一个函数是一个子程序,用于实现一串操作的代码块,它是完成特定任务的"黑盒子". 当 ...
- 新图形API为unity5 带来了什么&下一代新图形API的好处
西瓜的演讲ppt翻译+解释+其他: wolf96 在最基本的层面上,这些新api是为了改进CPU性能和效率,通过:减少CPU渲染瓶颈的情况,提供更多可预测和稳定的驱动的行为,给应用程序更多控制,就像在 ...
- Service的两种启动方法
刚才看到一个ppt,介绍service的两种启动方法以及两者之间的区别. startService 和 bindService startService被形容为我行我素,而bindService被形容 ...