[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.

时间: 2024-08-25 20:27:56

[RxJS] Stream Processing With RxJS vs Array Higher-Order Functions的相关文章

Javascript—Higher Order Functions

Higher order functions are functions that manipulate other functions. For example, a function can take other functions as arguments and/or produce a function as its return value. Such fancy functional techniques are powerful constructs available to y

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-minutes/ We have entered an era where competitive advantage comes from analyzing, understanding, and responding to an organization’s data. When doing

Storm(2) - Log Stream Processing

Introduction This chapter will present an implementation recipe for an enterprise log storage and a search and analysis solution based on the Storm processor. Log data processing isn't necessarily a problem that needs solving again; it is, however, a

Akka(23): Stream:自定义流构件功能-Custom defined stream processing stages

从总体上看:akka-stream是由数据源头Source,流通节点Flow和数据流终点Sink三个框架性的流构件(stream components)组成的.这其中:Source和Sink是stream的两个独立端点,而Flow处于stream Source和Sink中间可能由多个通道式的节点组成,每个节点代表某些数据流元素转化处理功能,它们的链接顺序则可能代表整体作业的流程.一个完整的数据流(可运行数据流)必须是一个闭合的数据流,即:从外表上看,数据流两头必须连接一个Source和一个Sin

13 Stream Processing Patterns for building Streaming and Realtime Applications

原文:https://iwringer.wordpress.com/2015/08/03/patterns-for-streaming-realtime-analytics/ Introduction More and more use cases, we want to react to data faster, rather than storing them in a disk and periodically processing and acting on the data. This

RxJS入门2之Rxjs的安装

RxJS V6.0+ 安装 RxJS 的 import 路径有以下 5 种: 1.创建 Observable 的方法.types.schedulers 和一些工具方法 import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent, SubscriptionLike, PartialObserver } from 'rxjs'; 2.操作符 operators import { map

【JAVA】merge two array by order

public class MergeSort { static void show(int a[]) { int i; for (i = 0; i < a.length; i++) { System.out.print(a[i]+"-"); } System.out.println("\n"); } static void merge(int arr1[], int arr2[], int res[]) { int i=0,j=0; int idx = 0;

[Redux] Understand Redux Higher Order Reducers

Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a new reducer. In that new reducer, you can customize the behaviour of the original one which helps reducing the reducer logic. In this lesson, we'll se

[React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component

In this lesson we'll create a Higher Order Component (HOC) that takes care of the key property that React looks for when using map to create elements from a list. This HOC will allow us to wrap any component and it will take care of placing the keypr