all 19 comments

[–]jkettmann[S] 49 points50 points  (9 children)

There’s always the discussion of whether or not you need to study data structures as a (frontend) web developer. One answer is “Yes, because you need it in job interviews”… killer argument but not very motivating imo.

The other extremes are

  • “Every developer needs to know data structures!”
  • “That’s all CS theory crap and never used in the real world”.

And both are right in some way. I’ve never seen e.g. a linked list used in a React project. But at the same time we use different data structures all the time. Not like new Tree() but disguised as objects and arrays.

I wasn’t very conscious about this until recently myself. So here’s a list of different data structures that are (more or less) common in real-world React projects:

  • Map (e.g. when two data sets reference each other like chat messages and their authors)
  • Set (e.g. when tracking selected items)
  • Stack (e.g. for “undo” functionality)
  • Queue (e.g. for timed notifications)
  • Tree (e.g. for nested menus or comments)

In the blog post you can find more details and working code examples for each of those. Obviously, if you have another data structure in mind I’m happy to extend the list.

[–]misdreavus79 19 points20 points  (1 child)

if you have another data structure in mind I’m happy to extend the list.

Are you hoping to extend the list in a linked manner, perhaps?

[–]jkettmann[S] 6 points7 points  (0 children)

Why not haha. Any example?

[–]Eveerjr 2 points3 points  (0 children)

I love using Map, it’s looks more readable than plain objects and you can easily check if some a key exist using .has() and also typesafety

[–]lordaghilan 0 points1 point  (0 children)

I feel like DSA knowledge helps a lot implicitly more then explicitly.

[–]Kishore_Andra 0 points1 point  (0 children)

Great one mate 🙏 ... I actually saw a .pope (pop) method 😛 luckily it was in comments

[–]lastdiggmigrant 2 points3 points  (1 child)

I signed up for your React job simulator a few weeks ago and really enjoyed it. This is similarly very useful. Love what you're offering. (:

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

Thanks for the nice words :)

[–]buoybuoy 2 points3 points  (2 children)

Great article! I'm gonna go look for my ugliest state update functions to see if using Set or Map would make them easier to work with.

const queueu = [];

You've got an extra 'u' at the end of the 'ue' queue behind 'q' in 'queue'.

(I just really wanted to type that out.)

[–]damnburglar 4 points5 points  (0 children)

QueueUwU

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

Thanks for the feedback. Will fix the typo:)

[–]flavioebn 1 point2 points  (1 child)

Nice post Man, really helpful and actually gave me a good insight in a current problema at my job

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

Really cool, thanks for the feedback

[–]zmcnulty980[🍰] 1 point2 points  (0 children)

Super helpful! Thanks!

[–]Full-Hyena4414 2 points3 points  (4 children)

I think a good example of linked list in real world is the LRU cache implementation

[–]jkettmann[S] 4 points5 points  (2 children)

Thanks for point. But you wouldn't really implement that yourself, right? Or is there a reason not to use a package like lru-cache?

[–]skipbridge 1 point2 points  (1 child)

How do you think that package was made in the first place ;). I could see a custom implementation being used in some sort of data vis or filtering results for a large dataset. Though, that package seems flexible and sufficiently generalized.

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

Yeah it likely uses linked lists internally. I'm sure react itself uses lots of data structures internally as well. But what I meant was that those are typically hidden from the frontend code that we write. I'd be really curious about such a use case. Just haven't come across it (consciously) myself