According to thisfor is pretty much always faster than forEach, so if performance is more important than concise code (and it usually should be) a for loop is likely a better choice.
Correct, as the forEach call will incur a penalty invoking the lambda expression. forEach could have equivalent performance if the JIT inlines the lamba method body, but it's safe to say always expect forEach to be a bit slower.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Do you know if
forEachis better in term of perf thanfor?Nice post !
According to this
foris pretty much always faster thanforEach, so if performance is more important than concise code (and it usually should be) aforloop is likely a better choice.Correct, as the
forEachcall will incur a penalty invoking the lambda expression.forEachcould have equivalent performance if the JIT inlines the lamba method body, but it's safe to say always expectforEachto be a bit slower.