A practice that I always tend to follow is to prefer O(1) over O(n).
What do I mean by that is that instead of using products: [], you would use products: new Map.
Now CRUD operations, in my opinion, could be more efficient. Instead of iterating over an array to retrieve an item(which, in the worst case, it takes O(n)), you could simply do products.get(productId), which has the complexity of O(1) .
One thing to note is that Map and Set are not reactive in Vue by default. There is a workaround.
I too prefer to just access a prop in an object rather than iterating over an array. Big O(1).
It gets worse if you're trying to write a complex algo for parsing data that comes from the server trying to give it some useful shape for the ui. (E.g. data for a table).
Sometimes I do iterate over an array (just one time) to convert it into a dictionary (an object).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A lot of useful tips! Thank you!
A practice that I always tend to follow is to prefer
O(1)
overO(n)
.What do I mean by that is that instead of using
products: []
, you would useproducts: new Map
.Now CRUD operations, in my opinion, could be more efficient. Instead of iterating over an array to retrieve an item(which, in the worst case, it takes
O(n)
), you could simply doproducts.get(productId)
, which has the complexity of O(1) .One thing to note is that Map and Set are not reactive in Vue by default. There is a workaround.
Cool!
I too prefer to just access a prop in an object rather than iterating over an array.
Big O(1)
.It gets worse if you're trying to write a
complex algo
for parsing data that comes from the server trying to give it some useful shape for the ui. (E.g. data for a table).Sometimes I do iterate over an array (just one time) to convert it into a
dictionary
(an object).