Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Is it good to use _.findWhere always?
Deepika Banoth
Deepika Banoth

Posted on

     

Is it good to use _.findWhere always?

We spent a lot of time optimizing our code to increase performance of UX of our app. But still, we thought it was not up to the mark as it was taking little more time to load UI whereas getting the response from API was in less than a second. We felt something was wrong.

We decided to check on a particular page what part of code is taking a lot of time using performance panel from chrome dev tool.

We were all astonished after looking at the result. It was_.findWhere which was taking a lot of time. We use underscore.js (a javascript library which provides helper functions) throughout our app. We used_.findWhere in some places where there is a better alternative which executes fast.

So is it bad to use_.findWhere?

The answer isNO.
There is nothing wrong in using_.findWhere, but you also need to know when to not use it.

Suppose you have an array of objects, let's sayexample_array, you want to find a particular object in that array only once. Then using_.findWhere is good.

But you have a scenario where you want to find different objects fromexample_array in different places of your controller, then using_.findWhere would be a bad idea. Why do you want to parse the same array by looping it again and again?

Instead, index the array and get the object immediately. You will get your required result in no time.

When we executed this change by replacing_.findWhere with indexed objects. Performance of our page was improved. The page was loading noticeably faster than before 🙂

So be sure when to use_.findWhere and when not to use.

I hope this post helps 🙂

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
chiangs profile image
Stephen Chiang
Design Technologist. Harmonizing design and development daily.Apply ☕ || 🍺 liberally.
  • Location
    Stavanger, Norway
  • Work
    UX engineer / Front-end consultant / Mechanical keyboard hobbyist
  • Joined
• Edited on• Edited

If you're only looking for the first instance of the object in the array, you can just use the native methodarray.find if you want all the instances of that object from the array you can usearray.filter ... I don't think you don't need lodash for that operation.

CollapseExpand
 
riturajcse profile image
Rituraj
  • Joined

Informative post indeed, but just a thought with ES 7 in hand now most of the things that we do using libraries like underscore or lodash can be done using new features of ES 7 in one way or another and in most cases these native JS functions will be faster. Yes but obviously as your post states this all varies scenario to scenario and hence we should keep our eyes open :) .

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Bangalore
  • Work
    Software Engineer R&D at JFrog India Pvt Ltd
  • Joined

More fromDeepika Banoth

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp