Skip to content

lmammino/js-iter-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

js-iter-examples

Code examples from my talk "JavaScript iteration protocols"

This talk has been delivered at JSNation 2023 (slides):

"JavaScript iteration protocols" talk cover


Prerequisites

Make sure to have a recent version of Node.js installed.

All the examples should work with Node.js 16 (but they have been tested with Node.js 20).

Install

Simply clone the repo and install dependencies with npm install.

Create some sample logs

To create a small sample log file, you can run:

node src/generate-log-file > logs.jsonl

To create a much bigger sample log file, you can run:

node src/generate-log-file 20000000 > logs-big.jsonl

Keep in mind that this operation might take a few minutes and that it will generate a big file (~3.6 GB).

Run the log processor scripts

Now you can run the different implementations of the log processor scripts:

This is the first implementation (eager).

You can run it with:

node src/process-eager.js logs.jsonl

But if you try to run it with the bigger log file:

node src/process-eager.js logs-big.jsonl

You should see an error!

This is the optimised version, using async iterators.

You can run it with:

node src/process-iter.js logs.jsonl

And it will also be able to process the bigger log file:

node src/process-iter.js logs-big.jsonl

... which might take a couple of minutes, depending on your device power.

This is a version of the previous script using .map(), .filter(), and .reduce() through core-js.

Check it out and compare the code with the previous implementation.

This is another version of the previous script that uses .map(), .filter(), and .reduce() but not through Async Iterators. Instead it uses a similar functionality through Node.js streams.

❤️ Thanks to @ErickWendel, for the awesome suggestion on this one!

Additional material

If you are interested in these kinds of topics, you might also enjoy:

(If you like them, please remember to give them a star and share them with your friends 😇)

Shameless plug 😇

If you like this piece of work, consider supporting me by getting a copy of Node.js Design Patterns, Third Edition, which also goes into great depth about generators, iterator protocols, streams and related design patterns.

If you already have this book, please consider writing a review on Amazon, Packt, GoodReads or in any other review channel that you generally use. That would support us greatly 🙏.

Contributing

In the spirit of Open Source, everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub or by submitting a PR.

License

Licensed under MIT License. © Luciano Mammino.

About

Code examples from my talk "JavaScript iteration protocols"

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published