N
N
Nikolay372018-11-28 03:09:16
JavaScript
Nikolay37, 2018-11-28 03:09:16

What will be faster using if or forEach?

Through the ws connection, 20-50 JSON requests (data) are received per second, each of which needs to be parsed, and after that either put a condition (if), whether it fits or not, or create an array and sort through all the received data through foreach, which will be faster ? Or maybe there is an even faster way?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Eremin, 2018-11-28
@Nikolay37

The answer is here:

Or create an array and use foreach to sort through all the incoming data, which will be faster?

You received a message, you put it in an array. Then another, then another. Then, for some reason (event, timer), forEach must be called.
- I.e. message, for some time just lies in the array without processing.
- And if you make a mistake and forget to clear the array after forEach, the processing time will grow with time.
- what if at the moment when forEach is executed, and you have not yet cleared the array, you will receive messages? They will go into an array and then be removed immediately
. The best recommendation would really be to look towards rxjs

A
atawerrus, 2018-11-28
@atawerrus

It's so insignificant that it's not worth worrying about.

A
abberati, 2018-11-28
@abberati

Look in the direction of rxjs, you will find a lot of useful things for yourself. I advise you to use streams for this task.

D
Dmitry, 2018-11-28
@dimoff66

Naturally, extra busting is extra time. It is obvious.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question