1

Is using .find() in JavaScript inefficient as compared to using .map() or .forEach() ?

For example, in case of an array of objects :

var arr=[{name:"vishek",ID:222,subjects:["science","maths","politics"]}]

For such an array, is it efficient to use .find to find a particular object or .map()?

Tom O.
  • 5,730
  • 2
  • 21
  • 35
P.B.UDAY
  • 465
  • 2
  • 10
  • 8
    Well, `find` returns the first match, so in that sense you likely end up iterating through fewer elements of the array than the other methods. – Nick Jan 18 '19 at 15:59
  • 5
    If you have a large amount of data and you need to perform lookups, the real answer is to choose a better data structure than a simple array. – Pointy Jan 18 '19 at 15:59
  • 6
    Neither `.map()` nor `.forEach()` are meant to only "find" an element. – Andreas Jan 18 '19 at 16:00
  • 4
    Each Array method has a use case. If you want to `find` one element in your array, then you use find. If you want to find `some` elements, then you use some, if you want to change the elements based with something else, you use `map`, etc. – Baruch Jan 18 '19 at 16:02
  • The easiest way to find the answer would have been to time the functions and find out your self. Like this for example: https://jsfiddle.net/gp018qrs/ – Olian04 Jan 18 '19 at 16:13

0 Answers0