When/why to use map/reduce over for loops

后端 未结 6 1173
情深已故
情深已故 2020-12-09 17:03

So I am getting into a bit of object manipulation in JavaScript for the first time and I have a question I\'m wondering if anyone could answer.

When I have an object

6条回答
  •  天涯浪人
    2020-12-09 17:52

    This is like asking if I like basketball or football better. Both have their positives.

    If you have 10 developers look at your for loop, 9 out of 10 will know what you are doing right away. Maybe half will have to look up what the map() method is, but then they'll also know what's going on. So in this respect, a for loop is easier for others to read.

    On the flip side, map() will save you two or three lines of code.

    As far as performance goes, you'll find map() is built internally with something akin to a for loop. You might see a few milliseconds of difference when it comes to performance speeds if you run them through large iterations; but they'll never be recognizable to an end user.

提交回复
热议问题