JavaScript Iteration Protocols

Rate this content
Bookmark

How many ways do you know to do iteration with JavaScript and Node.js? While, for loop, for...in, for..of, .map(), .forEach(), streams, iterators, etc! Yes, there are a lot of ways! But did you know that JavaScript has iteration protocols to standardise synchronous and even asynchronous iteration?


In this workshop we will learn about these protocols and discover how to build iterators and iterable objects, both synchronous and asynchronous. We will learn about some common use cases for these protocols, explore generators and async generators (great tools for iteration) and finally discuss some hot tips, common pitfalls, and some (more or less successful) wild ideas!

FAQ

The initial task assigned by the CTO was to help troubleshoot a production issue by analyzing log files to determine the impact on customers and count occurrences of a specific error for each customer.

The team approached the problem by using Node.js to read the log files, parse each line as JSON, filter to keep lines with specific errors, and then reduce the data to count occurrences per customer.

The next day, a 'Range Error: Invalid String Length' error occurred because the script attempted to load the entire content of large files into memory, which was not feasible for large log files.

The proposed solution was to process the log files line by line using streams and iterators, which avoids loading the entire file into memory and helps handle very large files efficiently.

Iterators are a fundamental concept in JavaScript that allow sequential access to the elements of a collection. They are important because they provide a mechanism to handle data one item at a time, which is efficient especially for operations on large or potentially infinite data sets.

Yes, generator functions can be used with asynchronous operations. By combining generators with async functions, you can handle asynchronous data flows more effectively, allowing for operations like pausing the function execution until data is fetched.

The async-iterable protocol allows for the handling of asynchronous data sources, such as streaming data or paginated API responses. It provides a structured way to process data chunks as they become available, which is crucial for performance and efficiency in web applications.

Luciano Mammino
Luciano Mammino
27 min
01 Jun, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

We are working on troubleshooting a production issue in a startup. The CTO identified a problem with loading large files into memory and suggested reading the file line by line. We learn about iterators and generators in JavaScript, which allow us to process data one item at a time. Generators can be used to combine async and generator functions for file processing. The speaker also discusses using a for loop instead of map, filter, and reduce. The Talk concludes with the speaker mentioning poly-filling the implementation using core.js and offering free workshops on iteration protocols and Node.js streams.

1. Introduction to the Problem

Short description:

We just started to work in a new startup. The CTO reached out to us for help with troubleshooting a production issue. We need to analyze log files, count occurrences of a specific error, and group them by customer.

So, thank you for the introduction. I know we are just after lunch, but I hope we can forget about that for a second and start a new adventure together. So this is basically the story. We just started to work in a new company, it's a startup, and this is our first day in this startup. And the first thing that happens is that the CTO reaches out to us asking, can you help me with troubleshooting a problem that I have? It turns out that it's an actual production issue. And the CTO is looking at some logs and is trying to figure out which customers are impacted by these logs. And the way that we can help the CTO is by helping him to analyze some log files, where we want to count how many are FCKD, I don't know what that means, that are in that particular file, and we want to count how many of them are happening for each and every one customer. So if we look at the log file, it looks more or less like this. We are interested in these three lines here, in this particular section, just because that particular error shows up. And then we want to take all these lines and count how many occurrences there are per every different customer. So this should be pretty easy, so let's work together on an implementation for this.

2. Reading and Processing Log Files

Short description:

We import the read file function from node.js to read the log file and store the information in the raw data variable. We then split the file into individual lines and create JSON objects for each line. After filtering for the specific error, we use reduce to group and count occurrences by customer. We provide the script to the CTO, who is initially impressed. However, the next day, the CTO identifies a problem with loading large files into memory. Instead, we should read the file line by line, filtering and aggregating the results as we go.

So we need to read log files. So the first thing that we do is we import read file from node.js, then we use that function to actually read the file, and this is going to load all that file information into this variable called raw data. Then we want to split on every line because eventually we want to process every single line individually.

Now we take this array of lines, we map it so that we can use JSON parts to actually create JSON objects for every single line. And at this point we can filter and just say, okay, just keep the lines where we have that message dot error equal to the specific error we are looking for. And finally, we can just do a reduce to group things, actually to count things by specific customers. And we just log the result of that reduce. And we should see something like this. Basically, we see an object where the keys are customer IDs and the values are the number of times that that particular error was happening for every single customer.

So we give this script to the CTO, the CTO is really happy and is telling us, great job, great first day, looking forward to seeing you tomorrow. So experience plus plus. All right. The next day, the CTO is telling us, well, you know that script you gave me yesterday? Actually, now there is a problem. I am seeing this error called range error invalid string length. Now I'm going to show you the code again. Can anyone tell me in which line we have a problem? Come on, don't be shy. Okay somebody's number three. Good guess. Why is this a problem? The problem is there because we are basically loading the entire content of the file in memory. We are just saying take all that data and store it into a variable as a string. And if the file starts to be relatively big, we are starting to talk about the order of gigabytes, you can start to see that particular problem. Or if it's over two gigabytes, you will see another problem, which is very similar. So the problem conceptually here is that we are preloading all the information in memory, which for big files is not something that is going to work. So what is an ideal approach instead? The best approach would be, OK, why not pass this file line by line? At the end of the day, we just care about reading one line at a time. We don't need to preload everything in memory. So an ideal implementation would look like this. We just load this one line, we pass it, we filter for it, and we start to aggregate the result. We get the next line and in this case, when we try to filter, we realize we don't care about this line, so we just get disregarded. We move it to the next line, again we pass, we filter, and we aggregate. And again, just to show you another example, we pass, we filter, aggregate, and in this case we find again the first customer, so we can just increment the counter to 2.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
Do you have a large product built by many teams? Are you struggling to release often? Did your frontend turn into a massive unmaintainable monolith? If, like me, you’ve answered yes to any of those questions, this talk is for you! I’ll show you exactly how you can build a micro frontend architecture with Remix to solve those challenges.
Full Stack Components
Remix Conf Europe 2022Remix Conf Europe 2022
37 min
Full Stack Components
Top Content
Remix is a web framework that gives you the simple mental model of a Multi-Page App (MPA) but the power and capabilities of a Single-Page App (SPA). One of the big challenges of SPAs is network management resulting in a great deal of indirection and buggy code. This is especially noticeable in application state which Remix completely eliminates, but it's also an issue in individual components that communicate with a single-purpose backend endpoint (like a combobox search for example).
In this talk, Kent will demonstrate how Remix enables you to build complex UI components that are connected to a backend in the simplest and most powerful way you've ever seen. Leaving you time to chill with your family or whatever else you do for fun.
Making JavaScript on WebAssembly Fast
JSNation Live 2021JSNation Live 2021
29 min
Making JavaScript on WebAssembly Fast
Top Content
JavaScript in the browser runs many times faster than it did two decades ago. And that happened because the browser vendors spent that time working on intensive performance optimizations in their JavaScript engines.Because of this optimization work, JavaScript is now running in many places besides the browser. But there are still some environments where the JS engines can’t apply those optimizations in the right way to make things fast.We’re working to solve this, beginning a whole new wave of JavaScript optimization work. We’re improving JavaScript performance for entirely different environments, where different rules apply. And this is possible because of WebAssembly. In this talk, I'll explain how this all works and what's coming next.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
As developers, we spend much of our time debugging apps - often code we didn't even write. Sadly, few developers have ever been taught how to approach debugging - it's something most of us learn through painful experience.  The good news is you _can_ learn how to debug effectively, and there's several key techniques and tools you can use for debugging JS and React apps.
Webpack in 5 Years?
JSNation 2022JSNation 2022
26 min
Webpack in 5 Years?
Top Content
What can we learn from the last 10 years for the next 5 years? Is there a future for Webpack? What do we need to do now?
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
You can check the slides for James' talk here.

Workshops on related topic

Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
React Day Berlin 2022React Day Berlin 2022
86 min
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
Top Content
WorkshopFree
Hussien Khayoon
Kahvi Patel
2 authors
Using a library might seem easy at first glance, but how do you choose the right library? How do you upgrade an existing one? And how do you wade through the documentation to find what you want?
In this workshop, we’ll discuss all these finer points while going through a general example of building a code editor using CodeMirror in React. All while sharing some of the nuances our team learned about using this library and some problems we encountered.
Testing Web Applications Using Cypress
TestJS Summit - January, 2021TestJS Summit - January, 2021
173 min
Testing Web Applications Using Cypress
WorkshopFree
Gleb Bahmutov
Gleb Bahmutov
This workshop will teach you the basics of writing useful end-to-end tests using Cypress Test Runner.
We will cover writing tests, covering every application feature, structuring tests, intercepting network requests, and setting up the backend data.
Anyone who knows JavaScript programming language and has NPM installed would be able to follow along.
Build a powerful DataGrid in few hours with Ag Grid
React Summit US 2023React Summit US 2023
96 min
Build a powerful DataGrid in few hours with Ag Grid
WorkshopFree
Mike Ryan
Mike Ryan
Does your React app need to efficiently display lots (and lots) of data in a grid? Do your users want to be able to search, sort, filter, and edit data? AG Grid is the best JavaScript grid in the world and is packed with features, highly performant, and extensible. In this workshop, you’ll learn how to get started with AG Grid, how we can enable sorting and filtering of data in the grid, cell rendering, and more. You will walk away from this free 3-hour workshop equipped with the knowledge for implementing AG Grid into your React application.
We all know that rolling our own grid solution is not easy, and let's be honest, is not something that we should be working on. We are focused on building a product and driving forward innovation. In this workshop, you'll see just how easy it is to get started with AG Grid.
Prerequisites: Basic React and JavaScript
Workshop level: Beginner
0 to Auth in an Hour Using NodeJS SDK
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher
Build a Powerful Datagrid With AG Grid
React Summit 2024React Summit 2024
168 min
Build a Powerful Datagrid With AG Grid
WorkshopFree
Brian Love
Brian Love
Does your React app need to efficiently display lots (and lots) of data in a grid? Do your users want to be able to search, sort, filter, and edit data? AG Grid is the best JavaScript grid in the world and is packed with features, highly performant, and extensible. In this workshop, you’ll learn how to get started with AG Grid, how we can enable sorting and filtering of data in the grid, cell rendering, and more. You will walk away from this free 3-hour workshop equipped with the knowledge for implementing AG Grid into your React application.
JavaScript-based full-text search with Orama everywhere
Node Congress 2023Node Congress 2023
49 min
JavaScript-based full-text search with Orama everywhere
Workshop
Michele Riva
Michele Riva
In this workshop, we will see how to adopt Orama, a powerful full-text search engine written entirely in JavaScript, to make search available wherever JavaScript runs. We will learn when, how, and why deploying it on a serverless function could be a great idea, and when it would be better to keep it directly on the browser. Forget APIs, complex configurations, etc: Orama will make it easy to integrate search on projects of any scale.