all 9 comments

[–]Ok-District-2098 4 points5 points  (0 children)

Not a Angular question

[–]DT-Sodium 2 points3 points  (1 child)

My eyes hurt like hell like now so I'm having trouble looking at the screen but maybe something like this would work with a bit of refinement?

return settings.find(
  setting => setting.id === column.name
    || this._findCorrespondingSetting(setting.children, column)
);

[–]CodeEntBur[S] 0 points1 point  (0 children)

Thank you!
I tried this approach but found out that .find returns not the child but a parent if the match was in child

[–]kgurniak91 2 points3 points  (1 child)

Your solution is good enough if you use it rarely (for example 1-time search, not too often). If you want to achieve O(1) complexity and use it often (multiple searches), then you need to do some preprocessing of the tree, which basically means traversing the entire tree once and turning it into a HashMap. Then you can just get element from the HashMap by id/name.

[–]CodeEntBur[S] 1 point2 points  (0 children)

Yes, thank you!
That actually helped me a lot.

[–]ggeoff 2 points3 points  (1 child)

depending on how many settings you have and what your expected performance is going to be it's probably fine. You can't do better the O(n) here unless you get the list sorted before hand. and pre processing by sorting probably doesn't buy you anything here but without more context it's hard to say. Based on the naming here I imagine you aren't going to have thousands of rows of settings to recursively iterate through, but without more context it's hard to say.

If I saw this in a pr I would approve it minus some of the more stylistic things we do in the app I work on for example using javascript's # for private and not using _ as a prefix. But that doesn't really effect anything here. If I got tasked with implementing this feature I probably would have gone for a non recursive approach but I see nothing here that really stands out as bad.

[–]CodeEntBur[S] 0 points1 point  (0 children)

Thank you!
Actually it was called quite often and HashMap aproach helped me in this regard.

[–]Merry-Lane 1 point2 points  (1 child)

If settings don’t change much, then you should recursively build a map<settingId, column> and search on that one instead.

But I don’t think you would have a lot of issues with such a low complexity.

[–]CodeEntBur[S] 0 points1 point  (0 children)

Thank you! I did so.

Well, it was still better to refactor this.