Discussion on: Front-end best practices (featuring Vue.js)

anduser96 profile image
Andrei Gatej

A lot of useful tips! Thank you!

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.

champi profile image
Daniel Sarmiento Cordero Author


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).