[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 ...
随机推荐
- Php会员权限
<?phpecho $uu=@array_sum(@$_POST['gr']);?><form action="" method="POST" ...
- Django 基础
Django 的路由系统 在 django 的 URLconf 配置文件 urls.py 中根据一个 URL 对应 views 的一个函数来处理用户的请求. 1.基本的 urls 对应 urlpatt ...
- 初级SQL开发汇总指南
汇总部分内容来自网络(作者 :zhtbs),比较基础的东西,能够了解比较基础的一些东西. Select语句概要 数据库中数据的提取(查询)使用select 语法,主要有以下几点作用 l 提取的数据 ...
- 解决VS2015无法调试dotnet core项目
dotnet core 1.0正式版和VS2015 update3安装后一直无法在VS中正常调试. 错误提示:The debugger's worker process (msvsmon.exe) u ...
- 学习Swift -- 构造器(上)
构造器(上) 构造过程是为了使用某个类.结构体或枚举类型的实例而进行的准备过程.这个过程包含了为实例中的每个存储型属性设置初始值和为其执行必要的准备和初始化任务. 构造过程是通过定义构造器(Initi ...
- log4j源码阅读
基于log4j1.2.17的源代码阅读 org.apache.log4j.xml.DOMConfigurator 类是log4j的xml配置文件初始化类 org.apache.log4j.Proper ...
- UFLDL教程之(一)sparseae_exercise
下面,将UFLDL教程中的sparseae_exercise练习中的各函数及注释列举如下 首先,给出各函数的调用关系 主函数:train.m (1)调用sampleIMAGES函数从已知图像中扣取多个 ...
- Contest 20140914 Mushroom写情书 字符串雙hash 後綴數組
0111:Mushroom写情书 查看 提交 统计 提问 总时间限制: 10000ms 内存限制: 256000kB 描述 有一天,Mushroom准备向他的GF表白,为了增加表白成功率,Mush ...
- 如何搭建一个独立博客——简明Github Pages与Hexo教程
摘要:这是一篇很详尽的独立博客搭建教程,里面介绍了域名注册.DNS设置.github和Hexo设置等过程,这是我写得最长的一篇教程.我想将我搭建独立博客的过程在一篇文章中尽可能详细地写出来,希望能给后 ...
- Internship
zoj2532:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1532 题意:有n个发射点,m个中继站,然后发射点会发射一些信息 ...