Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to perform 2 functions one after the other with forEach, without looping twice?

Does anyone know how can I do the same thing as that :

Player.list.forEach(player => {
    player.firstLayer();
});

Player.list.forEach(player => {
    player.secondLayer();
});

But looping it only one time to make it faster.

(I want to execute all firstLayer function of all players, and then execute secondLayer function, but without looping it Player.list two times)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks for answer.

>Solution :

This is not possible and there is no real difference in performance. The Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. There is no difference between running over the loop once or twice. The difference of O(n) vs O(2n) in terms of asymptotic complexity doesn’t exist since both have the "linear" growth rate.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading