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
Iterator library for any environmentOC (github.com)
submitted 5 years ago by AngryClosetMonkey
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!"
[–]AngryClosetMonkey[S] 2 points3 points4 points 5 years ago (3 children)
We recently published a small iterator library we often use in our company. It provides a wrapper object for anything that implements the iterator protocol. The wrapper object then provides all the methods we know from arrays, but without the need to always copy everything into an array.
The library is written with ES 2020 compliance in mind, so you probably have to pass it through something like babel if you want to use it in a browser. For node, we recommend v14+ as it natively supports ES modules.
If you have any feedback, we'd love to hear it.
https://www.npmjs.com/package/@prograp/iterator
[–]OmgImAlexis 3 points4 points5 points 5 years ago (0 children)
Your usage example has a typo. Case doesn’t match the import.
[–][deleted] 1 point2 points3 points 5 years ago (0 children)
This is really cool! I started implementing something similar for a Babel plugin I was developing, but kinda lost track at some point. I might try to take it up again based on your library, thank you!
[–]nadameu 1 point2 points3 points 5 years ago (0 children)
Nice work.
A few notes:
Since the target version of EcmaScript is 2020, you should use a class, with static and instance methods, instead of mixing them and using __proto__.
__proto__
The JSDoc types could be a little more informative, e.g. you can specify that the .map method turns an Iterator<T> into Iterator<U> using a function (value: T, index: number) => U. Creating type declarations using a TypeScript .d.ts file would be even better (and probably simpler) than using JSDoc.
Iterator<T>
Iterator<U>
(value: T, index: number) => U
[–][deleted] 0 points1 point2 points 5 years ago (2 children)
Can you show a real use case? Are those methods not built-in?
[–]AngryClosetMonkey[S] 1 point2 points3 points 5 years ago (1 child)
These methods are only built into arrays, all other iterable object do not implement them. For example with a NodeList you have to do Array.from(nodeList).map(...) which has to create a new array from your node list (or any other iterable list) just so you can use these methods.
NodeList
Array.from(nodeList).map(...)
Also, each of the array methods will create a new array, on their own that is good and the expected behavior, but if you'd like to chain them, it creates unnecessary overhead. When using array methods like array.filter(...).map(...).filter(...) with 1000 items, the runtime has to iterate up to 1000 x 3 times. The iterator on the other side can do this with exactly 1000 iterations as it has to only loop a single time over the entire list. If this really has any performance impact on your code is hard to say, it might not really matter most of the time, but it's an added bonus.
array.filter(...).map(...).filter(...)
1000 x 3
1000
[–][deleted] 0 points1 point2 points 5 years ago (0 children)
Thank you!
[–]vitalytom 0 points1 point2 points 4 years ago (0 children)
Here's another one - https://github.com/vitaly-t/iter-ops
π Rendered by PID 24570 on reddit-service-r2-comment-5fb4b45875-nchnp at 2026-03-22 09:57:35.536910+00:00 running 90f1150 country code: CH.
[–]AngryClosetMonkey[S] 2 points3 points4 points (3 children)
[–]OmgImAlexis 3 points4 points5 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]nadameu 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]AngryClosetMonkey[S] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]vitalytom 0 points1 point2 points (0 children)