Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is RxJS faster than imperative? [closed]

I'm newer in functional programming and functional reactive programming.

I read a lot of times the great power of functional reactive programming.

Okey; is readable, avoid side effects, etc.

But... I don't know how to improve my code in functional/reactive way to execute faster than imperative way. Is it possible? Maybe I miss something? Because in my functional programming code is iterating for every task: for filter, map, reduce... And this is slower. Is possible to do all things iterating once? Maybe using compose()?

Thanks.

Performance test: Imperative vs FP vs FRP

var array = [];
var i, l;

//INIT ARRAY
for (i = 0; i < 15000; i += 1) {
  array[i] = i;
}

// WITH IMPERATIVE
console.time("IMPERATIVE");
var sum = 0;
var a;
var result = [];
for (i = 0, l = array.length; i < l; i += 1) {
  a = array[i];
  if (a % 2 === 0) {
    result.push(a * 10);
    sum += a * 10;
  }
}
console.log(sum);
console.timeEnd("IMPERATIVE");

// WITH DECLARATIVE: FUNCTIONAL PROGRAMMING
console.time("FUNCTIONAL");
var r = array
  .filter(function(x) {
    return x % 2 === 0
  })
  .map(function(x) {
    return x * 10
  })
  .reduce(function(x, y) {
    return x + y
  })

console.log(r);
console.timeEnd("FUNCTIONAL");

//WITH DELARATIVE: FUNCTIONAL REACTIVE PROGRAMMING
console.time("REACTIVE RXJS 4")
Rx.Observable
  .fromArray(array)
  .filter(function(x) {
    return x % 2 === 0
  })
  .map(function(x) {
    return x * 10
  })
  .reduce(function(x, y) {
    return x + y
  })
  .subscribe(function(x) {
    console.log(x)
  });
console.timeEnd("REACTIVE RXJS 4");

console.time("REACTIVE RXJS 6")
rxjs.of(array).pipe(
    rxjs.operators.filter(function(x) {
      return x % 2 === 0
    }),
    rxjs.operators.map(function(x) {
      return x * 10
    }),
    rxjs.operators.reduce(function(x, y) {
      return x + y
    }))
  .subscribe(function(x) {
    console.log(x)
  });
console.timeEnd("REACTIVE RXJS 6");
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.1.0/rxjs.umd.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.1.0/rx.all.js"></script>

Output:

Output

like image 461
Aral Roca Avatar asked May 08 '16 21:05

Aral Roca


People also ask

Is RxJS fast?

RxJS 7.5. 4 observables are now faster than native generators, native async generators, just as fast as a hand-rolled iterable, and nearly as fast as a plain function with callbacks when tested in scenarios of emitting values as quickly as possible. Just to stress this though: YMMV!!

Can RxJS replace redux?

They are very different things. RxJS can be used to do Reactive Programming and is a very thorough library with 250+ operators. And Redux is as described on the github repo "Redux is a predictable state container for JavaScript apps". Redux is just a tool to handle state in apps.

What are the most outstanding features of RxJS?

4) What are the most outstanding features of RxJS? The Observer is an object with next(), error(), and complete() methods, which are called when we have to interact with the observable, i.e., the source interacts for an example button click, Http request, etc.

Is RxJS worth learning?

But it's definitely worth it. After finishing all levels you'll have learned RxJS for LIFE! You might want to do things step by step. If so, take your time, and don't rush up things.


1 Answers

the great power of functional reactive programming

is not really related to speed and performance in my opinion. There are two things here, functional programming and reactive programming and none of the benefits of the aforementioned programming paradigms have to do with execution speed.

At the extreme, if you want the best execution speed, assembly language for the specific processor/architecture you are writing against can't be beaten. Then C which is one those languages closer to the processor level is very performant and generally speaking compiled languages (because they turn your code into assembly language) are more performant than interpreted languages.

So the rational of choosing functional programming vs. imperative programming is not a one-criteria choice. Additional criterias are :

  • programmer productivity. That includes the time to write the code, but also the time to review and understand the code, to detect and eliminate bugs, to extend the functionalities of that code (maintanibility and extensibility).
  • quality assurance. Some paradigms are better at ensuring the lower prevalence of some kind of bugs they specialize against and which constitute a major source of distraction and quality loss. There you have to introduce another distinction which is statically-typed vs. dynamically-typed languages. Some languages specialize at handling concurrency concerns (concurrent access to shared state), others distributed system concerns (fault tolerance) etc and focus on reducing the kind of bugs related to those concerns.

I could go at length with the different advantages of this and that but I don't want to repeat what is already omnipresent in the computer science litterature. The Programming Languages: Principles and Paradigms book is a good read but there are many others.

So back to your question, writing something with Rxjs is not necessarily any faster than imperatively (though really, your Rxjs code is still imperative code, but let's not mention that) though it can still be in some situations. Then, it does not make so much sense to use rxjs just for operations on array. Rxjs (and reactive programming) shines when there is a high level of asynchronocity which simply is not there for simple operations on array.

like image 114
user3743222 Avatar answered Oct 13 '22 11:10

user3743222