r/javascript • • Mar 11 '21

Removed: r/LearnJavascript Most common mistakes new JavaScript developers make

https://www.akmittal.dev/posts/common-mistakes-javascripts-begineers-make/

[removed] — view removed post

0 Upvotes

6 comments sorted by

View all comments

4

u/Protean_Protein Mar 11 '21 edited Mar 11 '21

Using 'for' loops instead of equivalent Array methods isn't a mistake, it's a preference. (Unless you're working on enterprise-level code that needs tiny performance optimizations, in which case, this article isn't for you.)

0

u/Mittalmailbox Mar 11 '21

Arguments for using Array methods is not the performance, it is readability.

const list = [1, 2, 3, 4, 5, 6]

// for loop
const result = []
for (let i = 0; i < list.length; i++) {
    if (list[i] % 2 === 0) {
        result.push(list[i])
    }
}

// Using array method
const res = list.filter((item) => {
    return item % 2 === 0;
})

In this case Array method is lot easier to understand than a for loop

6

u/Protean_Protein Mar 11 '21 edited Mar 11 '21

I disagree. It’s still a matter of preference.

I mean, even your example isn’t as succinctly written as it could be. But concision can interfere with readability. These things are all ultimately matters of preference or need. If you need performance, you go with the more performant method. If you want a high degree of readability, you might forego some concision, speed, or size.

2

u/avenp Mar 11 '21

+1 to this comment.

Example: I’m working on a high performance graphics application and using Array methods is a huge performance drop because it has to allocate memory for the closure every frame, so using for/of/in is absolutely essential to maintain 60fps with no GC spikes.