kefirjs / kefir

A Reactive Programming library for JavaScript
https://kefirjs.github.io/kefir/
MIT License
1.87k stars 97 forks source link

Is transforming a stream (and not adding a subscriber) expensive? #201

Closed frankandrobot closed 8 years ago

frankandrobot commented 8 years ago

Here's the use case:

foo() {
   //dispatch a message
   setTimeout(() => AppDispatcher.emit('foo'), 0) 
   //return a new stream. We don't always subscribe to this stream
   return AppDispatcher
        .map(values => //...)
        // maybe more transformations
        .take(1)
}

//...
foo() //just dispatch a message
foo().onValue(//... ) // subscribe to resulting stream

Imagine that this pattern happens everywhere in the app. Is transforming a stream (i.e., creating a stream from an existing stream) so expensive (in terms of memory, CPU) that I should be doing that only when I know it will have a subscriber?

[1] yea, I know kefirEmitter was deprecated but as per discussion in the announcement to remove it, sometimes you need a bus (like for example, when you're replacing Flux with Kefir)

rpominov commented 8 years ago

Honestly, I don't know. I didn't make any measurements of performance for creating/transforming observables. And generally this operations wasn't considered as hot path, so wasn't so optimized as event delivery path. It might be expensive, yes.