Is RxJS faster than imperative? [closed]

2019-04-09 21:16发布

问题:

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")
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");
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.1.0/rx.all.js"></script>

Output:

回答1:

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.