[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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具