Reputation: 85
I would like to filter entries in an array that select only from stocks ('Common Stock') and in NYSE and NASDAQ exchange. I have 45,000 entries in array and want to write something in JavaScript that filter the fastest and most efficient way.
I have tried .filter()
and it's working alright.
const searchUSData = [
{
"Code":"^DJI",
"Name":"Dow Jones Industrial Average",
"Country":"USA",
"Exchange":"INDEX",
"Currency":"USD",
"Type":"Common Stock"
},
{
"Code":"AAAAX",
"Name":"DEUTSCHE REAL ASSETS FUND CLASS A",
"Country":"USA",
"Exchange":"NMFQS",
"Currency":"USD",
"Type":"Mutual Fund"
},
{
"Code":"AAAIF",
"Name":"Alternative Investment Trust",
"Country":"USA",
"Exchange":"OTCGREY",
"Currency":"USD",
"Type":"Common Stock"
},
{
"Code":"AACS",
"Name":"American Commerce Solutions, Inc",
"Country":"USA",
"Exchange":"PINK",
"Currency":"USD",
"Type":"Common Stock"
},
{
"Code":"AAAIX",
"Name":"STRATEGIC ALLOCATION: AGGRESSIVE FUND I CLASS",
"Country":"USA",
"Exchange":"NMFQS",
"Currency":"USD",
"Type":"Mutual Fund"
},
{
"Code":"AAALF",
"Name":"Aareal Bank AG",
"Country":"USA",
"Exchange":"PINK",
"Currency":"USD",
"Type":"Common Stock"
}
]
// Use only stocks
// Use only stocks and NYSE / NASDAQ
const filterStockSearch = searchUSData.filter((item) => (
item.Type === 'Common Stock' &&
(item.Exchange === 'NYSE MKT' || item.Exchange === 'NASDAQ')
))
Upvotes: 1
Views: 1022
Reputation: 6767
Array.prototype.filter()
is the standard and usual way of doing this. You should consider code readability and maintainability, apart from performance, so even if you could do something else, this is probably the best option for a shared codebase.
Apart from that, looking at your code, I don't really see much that can be done to make it faster. One of the key considerations in a .filter()
is the filtering function. Based on just that, you should evaluate the condition you use to make it faster. As far as I can tell from the provided example, you are already using an AND (&&
) operator with a couple of conditions and it seems like the order is alright. However, if you have say 5 conditions, you should try and short-circuit the condition evaluation so that the first condition in your AND operator is the one that returns false
most often.
Additionally, as others have mentioned in the comments, this data seems to be generated or retrieved from elsewhere (i.e. a database). You could try ordering and/or filtering before you even retrieve the data, as there are other costs related to this data being on the client's machine or even a server (memory for storing, CPU for filtering etc.).
Finally, you can try and split the array into two arrays - or retrieve it as such - and try using async
-await
or promises to filter them in parallel. This might be faster, but you need to handle it carefully, as it is a little bit different from what you have right now.
Upvotes: 2