admin管理员组文章数量:1026989
So the gist of my question. Imagine you have a service that handles 2-3-4-10 actions. And to municate in several ponents, you have 2-3-4-10 Subjects.
So, is it better to have 1 subject, and pass in on next an object identifying which of the actions it relates to, and filter inside your subscription...or have the lot of them and subscribe separately?
How many subjects is too many? They more or less remain active all at once throughout.
Kind of curious in an abstract a sense as possible, rather than my own usecase and whether or not it could be done better.
So the gist of my question. Imagine you have a service that handles 2-3-4-10 actions. And to municate in several ponents, you have 2-3-4-10 Subjects.
So, is it better to have 1 subject, and pass in on next an object identifying which of the actions it relates to, and filter inside your subscription...or have the lot of them and subscribe separately?
How many subjects is too many? They more or less remain active all at once throughout.
Kind of curious in an abstract a sense as possible, rather than my own usecase and whether or not it could be done better.
Share Improve this question edited Oct 7, 2020 at 22:17 Rafi Henig 6,4422 gold badges21 silver badges41 bronze badges asked Oct 7, 2020 at 19:57 Vlad PinteaVlad Pintea 8538 silver badges26 bronze badges 3- 2 IMO there is nothing wrong with having multiple subject, as long as they have a good purpose. I wouldn't mix their business logic for the sake of reducing their number. A subject is also an observable and when you use an operator, then you have an observable + a subscriber when the entire stream is subscribed. – Andrei Gătej Commented Oct 7, 2020 at 20:20
- that's mostly my thought as well, but I am really curious if someone has experience with them just being too much, maybe in a huge project or something, I dunno. Might have my reviewer who's not a big fan of rxjs proposing it is a performance concern and to rewrite as one and wanna defend it with...actual data if it's available :)) and this is easier than writing it in 2 ways and trying to pare – Vlad Pintea Commented Oct 7, 2020 at 20:29
- 3 I doubt that you'd ever experience performance hits due to using too many Subject. I'm quite familiar with the source code and I'd even say that a long chain of operators will consume more(not notably) than a Subject instance. – Andrei Gătej Commented Oct 7, 2020 at 20:33
1 Answer
Reset to default 8I work on large angular applications that use hundreds if not thousands of subjects (Subject, BehaviorSubject, ReplaySubject, AsyncSubject).
Is there a performance hit for using many subjects?
To this, I'd say it's not the subjects that matter, since they are only taking up memory space. It's the pipeline that you attach to them, which places them into the cpu's putation queue that matters. This is dependent on the pipelines themselves, and not the subjects. You could have a single subject that connects to a long putational heavy pipeline, which if done incorrectly, would slow your program down since javascript runs on a single thread of execution (you could use web workers to avoid this problem).
Therefore, the number of subjects is irrelavent here if we are talking about how "performant" your application is. It's the pipelines that determine if your application is slow. Ie, data moving down a pipe and having operators manipulate it.
StackBlitz single pipe that is putationally heavy to prove my point.
Is it better to have 1 subject, and pass in on next an object identifying which of the actions it relates to, and filter inside your subscription?
I would say this is more of a design decision to have a bus of information ("a single subject") passing all your data along, instead of breaking them up into their respective streams. This could be handy if your data is interconnected, meaning your events depend on each other, and if the order they appear within the stream matters (like navigation events: started, performing, ended, etc).
I would be unhappy if a dev used one giant bin to place all their data into instead of breaking it up into respective streams. Ie, if I have a user object, pany information, and notifications, I'd expect these to have separation of concerns, and to not be delivered through a bus system (a single subject), but to instead be delivered through different services, with their own respective subjects and pipelines.
How many subjects is too many? They more or less remain active all at once throughout.
If you're doing trivial maps and filtering, then don't worry about how many subjects you're using. Worry about if your streams of data make logical/logistical sense, and that they are structured/siloed correctly.
StackBlitz program bining 1 million behavior subjects to prove my point.
So the gist of my question. Imagine you have a service that handles 2-3-4-10 actions. And to municate in several ponents, you have 2-3-4-10 Subjects.
So, is it better to have 1 subject, and pass in on next an object identifying which of the actions it relates to, and filter inside your subscription...or have the lot of them and subscribe separately?
How many subjects is too many? They more or less remain active all at once throughout.
Kind of curious in an abstract a sense as possible, rather than my own usecase and whether or not it could be done better.
So the gist of my question. Imagine you have a service that handles 2-3-4-10 actions. And to municate in several ponents, you have 2-3-4-10 Subjects.
So, is it better to have 1 subject, and pass in on next an object identifying which of the actions it relates to, and filter inside your subscription...or have the lot of them and subscribe separately?
How many subjects is too many? They more or less remain active all at once throughout.
Kind of curious in an abstract a sense as possible, rather than my own usecase and whether or not it could be done better.
Share Improve this question edited Oct 7, 2020 at 22:17 Rafi Henig 6,4422 gold badges21 silver badges41 bronze badges asked Oct 7, 2020 at 19:57 Vlad PinteaVlad Pintea 8538 silver badges26 bronze badges 3- 2 IMO there is nothing wrong with having multiple subject, as long as they have a good purpose. I wouldn't mix their business logic for the sake of reducing their number. A subject is also an observable and when you use an operator, then you have an observable + a subscriber when the entire stream is subscribed. – Andrei Gătej Commented Oct 7, 2020 at 20:20
- that's mostly my thought as well, but I am really curious if someone has experience with them just being too much, maybe in a huge project or something, I dunno. Might have my reviewer who's not a big fan of rxjs proposing it is a performance concern and to rewrite as one and wanna defend it with...actual data if it's available :)) and this is easier than writing it in 2 ways and trying to pare – Vlad Pintea Commented Oct 7, 2020 at 20:29
- 3 I doubt that you'd ever experience performance hits due to using too many Subject. I'm quite familiar with the source code and I'd even say that a long chain of operators will consume more(not notably) than a Subject instance. – Andrei Gătej Commented Oct 7, 2020 at 20:33
1 Answer
Reset to default 8I work on large angular applications that use hundreds if not thousands of subjects (Subject, BehaviorSubject, ReplaySubject, AsyncSubject).
Is there a performance hit for using many subjects?
To this, I'd say it's not the subjects that matter, since they are only taking up memory space. It's the pipeline that you attach to them, which places them into the cpu's putation queue that matters. This is dependent on the pipelines themselves, and not the subjects. You could have a single subject that connects to a long putational heavy pipeline, which if done incorrectly, would slow your program down since javascript runs on a single thread of execution (you could use web workers to avoid this problem).
Therefore, the number of subjects is irrelavent here if we are talking about how "performant" your application is. It's the pipelines that determine if your application is slow. Ie, data moving down a pipe and having operators manipulate it.
StackBlitz single pipe that is putationally heavy to prove my point.
Is it better to have 1 subject, and pass in on next an object identifying which of the actions it relates to, and filter inside your subscription?
I would say this is more of a design decision to have a bus of information ("a single subject") passing all your data along, instead of breaking them up into their respective streams. This could be handy if your data is interconnected, meaning your events depend on each other, and if the order they appear within the stream matters (like navigation events: started, performing, ended, etc).
I would be unhappy if a dev used one giant bin to place all their data into instead of breaking it up into respective streams. Ie, if I have a user object, pany information, and notifications, I'd expect these to have separation of concerns, and to not be delivered through a bus system (a single subject), but to instead be delivered through different services, with their own respective subjects and pipelines.
How many subjects is too many? They more or less remain active all at once throughout.
If you're doing trivial maps and filtering, then don't worry about how many subjects you're using. Worry about if your streams of data make logical/logistical sense, and that they are structured/siloed correctly.
StackBlitz program bining 1 million behavior subjects to prove my point.
本文标签:
版权声明:本文标题:javascript - Rxjs Subject performance - how many is too many? Is it better to try to combine several into one? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745513918a2153951.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论