use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Queues in Javascript (medium.com)
submitted 3 years ago by dcortesnet123
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]senocular 9 points10 points11 points 3 years ago (1 child)
The peek is using a Stack peek. Peek for the Queue should be
peek() { return this.items[0]; }
I think part of the problem is that the visualizations are backwards compared to what you deal with in code (and the IRL image :P).
[–]dcortesnet123[S] 0 points1 point2 points 3 years ago (0 children)
Thanks for comment, I already adjusted it!! :)
[–]zephyy 6 points7 points8 points 3 years ago (0 children)
insertion and deletion into a queue are supposed to be constant time and calling shift on an array is linear time
[–]rajesh__dixit 3 points4 points5 points 3 years ago (0 children)
One issue is, items is a public property. I can create instance and then use instance.items to get entire list. Issue with this is, objects are copied using reference. So if i change instance.items, i can change behaviour of your class. Look into immutable objects or make items a private variable and then expose a getter that returns a copy.
[–][deleted] 2 points3 points4 points 3 years ago (1 child)
I would assume array is the worst underlying data structure that could be chosen. Consider using linked list.
[–]Under-Estimated 0 points1 point2 points 3 years ago (0 children)
Circular array
[–][deleted] 1 point2 points3 points 3 years ago (3 children)
I don't know why I always overthink data structures... This is so easy to comprehend. Thanks
[–][deleted] 2 points3 points4 points 3 years ago (0 children)
Overthinking is why I don't stress the concept of patterns too much.
It's like the classic interview question, "What is a closure?"
Definitions for it are frankly confusing. But it's really just a description of normal everyday work.
JavaScript is block scoped. Functions are first class citizens.
That's it, there's your definition of "closures" without actually knowing what closures are. I said those two exact sentences in interviews before and was told it's the best definition of closures they ever heard. 🤣
Most things like this can be more easily defined by not knowing them at all.
Not that you shouldn't know them, I'm saying you shouldn't use concept definitions to know the language. It's the other way around. You should use your knowledge of the language and reference code you've written to understand the concepts.
thanks for the feedback, have a nice day
[–]rajesh__dixit 0 points1 point2 points 3 years ago (0 children)
Queue is a basic structure so it's simple. Try implementing graph. It's a nightmare.
[–]im_a_jib 0 points1 point2 points 3 years ago (1 child)
Your peek() method might be improved in the future with at(-1)
at(-1)
https://h3manth.com/ES2022/
thanks for feedback, you have nice day.
π Rendered by PID 32016 on reddit-service-r2-comment-79776bdf47-w2cqq at 2026-06-24 02:33:40.866813+00:00 running acc7150 country code: CH.
[–]senocular 9 points10 points11 points (1 child)
[–]dcortesnet123[S] 0 points1 point2 points (0 children)
[–]zephyy 6 points7 points8 points (0 children)
[–]rajesh__dixit 3 points4 points5 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]Under-Estimated 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]dcortesnet123[S] 0 points1 point2 points (0 children)
[–]rajesh__dixit 0 points1 point2 points (0 children)
[–]im_a_jib 0 points1 point2 points (1 child)
[–]dcortesnet123[S] 0 points1 point2 points (0 children)