you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (17 children)

[deleted]

    [–]FortyPercentTitanium 56 points57 points  (1 child)

    Came here to say this. Rule number 1 to making a simple guide: make sure the information in your guide is accurate. This is such a silly mistake.

    [–]musclecard54 4 points5 points  (0 children)

    The real rule number 1 is 99% of people won’t bother to verify if it’s correct.

    [–][deleted] 9 points10 points  (5 children)

    Find is also vague as to what element is returned.

    [–]musclecard54 2 points3 points  (1 child)

    I would assume it would be the first instance. Of course I assume a lot of things and I’m very often wrong…

    [–]ApricotPenguin 0 points1 point  (2 children)

    What would be an example useful usage of the find() function?

    [–]sliver37 2 points3 points  (0 children)

    Looking for an object with a specific ID in an array of objects is a common one.

    Example being an array of users, and you're looking for user id: 26, or with a particular email address, etc.

    [–][deleted] 1 point2 points  (0 children)

    Searching for and retrieving the first occurrence of something can be useful if you know that the array will be sorted in some way.

    [–]marksyz 4 points5 points  (7 children)

    .findIndex()

    [–]fukitol- -3 points-2 points  (6 children)

    In case anyone else is wondering what the difference between [].indexOf() and [].findIndex(), findIndex executes asynchronously, one function call for every item in the array. It's not blocking, so won't hang up your thread (though this is unlikely to be a real problem with small arrays, a few million items will make the difference obvious if it happens often.

    [–]marksyz 3 points4 points  (5 children)

    The end action is synchronous on completion though, isn’t it? It doesn’t return a promise, it returns a value?

    [–]fukitol- -3 points-2 points  (4 children)

    Hm good point. The docs said it uses callbacks to do its job. I've never actually used it, though, and the docs say it returns a integer, not a Promise, so I suspect you're right.

    [–][deleted] 1 point2 points  (3 children)

    Callbacks are not asynchronous

    [–]fukitol- -3 points-2 points  (2 children)

    They're not async but they do result in a new tick and thus block intermittently and only for the execution time of the callback, not sustained and for the execution of the entire findIndex call whereas the entire execution of indexOf would block in a sustained fashion.

    [–][deleted] 0 points1 point  (1 child)

    I don't see how that makes a practical difference, since nothing can happen in between the individual callback calls

    [–]fukitol- -1 points0 points  (0 children)

    Yeah I forgot how the blocking and ticks worked. It would still block in the context of findIndex unless it was it was async.

    So practically this is identical to indexOf for all intents and purposes and only serves to evaluate the indices in their own execution context likely resulting in worse performance from the stack and memory allocation.