 My name is Liz Parody, and today I'm going to talk about JavaScript Parody, a fun journey through hot topics of Node.js and JavaScript. And before I start my talk, I will answer a question that many of you might be thinking right now. A lot of people come to me and they ask me, like, Liz, seriously, is Parody your last name? And I'm like, yes, it is. And that's the story of my life. That's it. Thanks for coming to my TED Talk. A little bit about me. I'm head of developers relations at Node.js, and if you haven't heard Node.js, we're having two workshops to today's, two workshops tomorrow here at the conference, so you're super welcome to visit. I'm from the beautiful and tropical country of Colombia. We don't have seasons there. So this weather is quite interesting for me. It's kind of cool, you know, like, a unique, like, time experience for a Colombian. I'm also organizer of Pioneras Developers, which is the biggest community for woman and tech in the country, and also organizer of JSConf Colombia and community leader next year. In Colombia, there is, like, a lot of events and conference happening, so you're super welcome to visit because it's a great place, and the community, it's awesome. And a fun fact about me, I want to visit 30 countries before I turn 30, so I have, like, two years and 25 countries more to visit, so it's made luck. Okay, so first of all, what is a Parody? A Parody is a piece that copies the style of a serious topic in a way that is intentionally humorous. It can include ridiculous mutations or exaggerations, or it's, in other words, simply a joke. Also, Parody is a very common last name in Italy, and when my ancestors arrived to Colombia like 200 years ago, they were like, oh, let's change the eye for a why, because, you know, it's more fun than that's the story of my last name. Now, let's talk about JavaScript. I'm going to go through four topics of Node.js and JavaScript that can be a little technical and complex to understand, but they're very useful to know and to work with. Let's begin. So number four, Stream. Streams have the reputation of being very hard to work with and even harder to understand. In the words of Dominic Tar, who created a very popular rep on Stream, Streams are the Node best and most misunderstood idea. Even Dan Abramov, creator of Redux and courting member of React, is afraid of Node Streams. It's like, what hopes do we have, right? But don't worry, we can figure this out. So let's start with the basics. When you tell a program to read a file in a traditional way, that file is read into memory from start to finish, and then it processes it. With the Stream, it's quite different. That file is read piece by piece, processing its content without keeping it into memory. It's also used to read or write input into output sequentially, and they're very useful to work with very large amounts of data. Let's think of a big file. Let's imagine, and because this is the parody of JavaScript, let's imagine that this file, it's just billion, trillion megabytes of data, like it's a very, very large file. And in this case, the file cannot be read in the memory, so it needs to read piece by piece and process its chunk. The thing about streaming services such as YouTube or Netflix, they don't deliver all the information and videos of thousands of ones. Instead, it's a continuous flow or chunks of information that allows users to start watching and listening almost immediately. So why stream? Mainly for three reasons. One is memory efficiency. You don't have to load the file into the memory in order to process it. Time, it takes significantly less time to process the file once you have it, instead of having to wait for the entire payload to be transmitted. And the number three reason, and most important, is because modern problems require more solutions. So, yes, so what happened when we have so many, many megabytes of data? The streams are a great solution for that. The main module is a native module that is shipped by default in Node.js, and all the streams are instances of event emitter class, that is, the streams are inherently event-based. They emit events that can be used to read or write data. There are four types of streaming Node.js. One is readable that is used to write data sequentially. Readable, guess what this does? Yes, it's used to read data sequentially. Duplex is used for both, like, to write and read data sequentially. And transform is when data can be modified, when it's written or writing. Like, for example, let's think about compression of files. We can write compressing of files, and we can read the compressed data using a stream. So now, let's do a little demo. To write a file using a stream, I'm using the FS module here. And to write a write stream, we just use create write stream. Very convenient. And we're going to create a file that is called the big file. This is going to write the streams a million times. The streams are farmed from zero to one million. And then it's going to end the process. So if I just do, like, Node writes file, it's going to take a few seconds. And then we can see a new file here, which is... It has one million in line for codes that says streams are fine. So this is a very, very large file. So how do we read that file? In the traditional way, just to read the file, we can do something like this. Like, I'm using the FS module here. I'm going to create a server that is going to read that big file. I'm going to handle the errors here. And the process, and this will be listening in the port 8000. So I just do something like this. Like, Node, read file. And then I'm going to be listening to the port 8000. So this is going to read a million times the streams were fine. And if we check the activity monitor here, wait for it. So it's consuming 475 megabytes of data. This is, like, a lot of megabytes' consumption in the memory. So, but there is, like, a better way of reading that same file. Let's stop this. And then how... Using streams to read the same file. I'm creating a server here. I'm using streams. Then from is a file. So can you use a think-await? Then to create a read stream, we use create read stream. And this is going to read the big file we just created. I'm using pipeline here because it's more safe than using just, like, pipe. Here we have the source and the response. And once it's finished reading the file, it's going to console log pipeline succeed. This is also going to be listening in the port 8000. So I'm going to stop this. And if I do Node, read file, pipeline. This is using streams. And then I just connect it to the server. I can see, wait for it, wait for it. One second. Now 27 megabytes of data using streams. So it's, like, a very good way of memory efficiency using streams. So that's how you can read and write data using streams. Now, if you want to learn more about streams with more examples and more in depth, guess what? I created a blog post called understanding streams in Node.js that you can find in Node.source blog posts. So you can check that out. Number three of hot topics of Node.js and JavaScript is worker threads. So to understand workers, first, it's necessary to understand how Node.js is structured, how does it work underneath. And I know what you might be thinking right now. You would be like, really, tell me more. I tell you more. Don't worry. So when a Node.js is launched, it runs one process in one thread. Being single thread means that one set of execution of instructions is executed at a time. One event loop is what allows JavaScript to be asynchronous, but of loading operations to the system kernel whenever possible, through async weights, callbacks, and promises. And one JavaScript instance is the program that executes JavaScript code, and one Node.js instance is a program that executes Node.js code. Basically, in other words, JavaScript cannot multitask like this SpongeBob. There is only one thing at a time happening at the event loop. And this one, as everything in live, it has a down size. Like, what is the down size of the regular process of Node? It is when you happen, when you have CPU intensive code, that means when you have a very large calculation happening in the memory, it can block other processes of being executed, and it can block the event loop and then crash everything we're doing. And we remember the golden rule, which is, keep calm and don't block the event loop, because that is not a good practice, so don't block the event loop. So it's important to differentiate between CPU operations and input-output operations. So when Node.js doesn't run in parallel, only input-output operations are run in parallel because they are executed asynchronously. So worker threads only help in CPU operations, not input-output operations. For that, we have asynchronous operations. So how is the standard process of worker threads? So before, in a traditional way, it's like one process, one event loop, one JavaScript instance, one Node.js instance, but with worker threads is a little bit different. So you have one process, and then you have multiple threads, and then you have one event loop per thread, and then you have one JavaScript instance per thread, and one Node.js instance per thread. So this allows to never block the event loop. And also, those threads are independent from each other, so you can stop one thread without necessarily having to stop the main thread. Now, let's do a little demo with worker threads. So here, I'm importing worker from worker threads, and what this does, new worker, is basically creating a new thread. This code is listening for a message from PoweringPort, and once it's received the message from PostMessage, it's going to throw the message back into the main thread. So basically, I'm using worker threads to send message back and forth. So the message will be, I mean, worker threads, yay, it's that message. But this is a JavaScript part of this, and instead of sending just a simple message, let's send a joke. So joke, and the joke is this. So there is different ways of laughing at the word, right? For example, when you laugh in English, you laugh like this, ha ha ha. When you laugh in Spanish, you laugh like this, ha ha ha. And laughing in JavaScript, you laugh like this. Yes, yes, yes, yes. So this is how we can send a joke through worker threads using worker threads. And this joke has a story behind, she was my previous boss, and then I was texting Truslak with her, and she said something funny, I laugh, and then I was like, oh, I'm sorry, I was laughing in Spanish, and she was laughing in Spanish, what is that? So this is how I came up with this joke. So thank you, Amelia, for reminding me of this joke. You wanna hear another joke? Oh, that one. Yes, no, yes? Okay, good you say yes, because I wouldn't know what to do if you say no. Other joke would be like, knock, who's there? So this is how we can also send another joke using worker threads. Yay, thank you, Kate, for sending me this joke. If you want to learn more about worker threads and more examples and with more in-depth, guess what? I also created a blog post called Understand the Worker Threads in Node.js. You can find a NodeSource blog post. Now, number two of hot topics of Node.js and JavaScript, ECMAC script modules. So back into 2017 when Node.js, Node version 8.9 was launched, it was launched experimental support for ECMAC script modules that are known for the import and export statements. This support was behind the flag experimental modules, so you needed to use that flag to use ECMAC script modules. But a few weeks ago, Miles Born, who's just spoke like 20 minutes ago, he said that in the latest version of Node, you can have on flag modules. That means that you don't have to use the flag anymore. Yay, no more flags. ECMAC script module is still in an experimental phase and it doesn't have the flags anymore. It's still subject to change, so yeah. Now a little overview about ECMAC script modules. All major browsers support ECMAC script modules via the tag script type module. Also, experimental support for ECMAC script module is enabled by default. Other runtimes and environments support ECMAC script modules, so that's why it's important for Node to adopt the standard as soon as possible. And last one, if you add type module to the package.json, the Node will treat all your JS files at ECMAC script modules. And this last point is so important that it deserve like a slide of its own. So there you go, as I know it's his own. So if you add type module to the package.json for your JS project, Node.js will treat all JS file in projects at ES module. Super important. So there are four main ways to reference a file. One is relative URL, like if you just put the parody file there. Then absolute, which will include the entire path of the file. The package.name, which will be only the name of the package, and if you have a package called a parody, I will be very curious to see it. And paths within package will be like the name of the package, and then a file that is inside the package. Also, import segment that referenced ECMAC script modules can use default export, which will be import underscore from the package. And this will import everything in that package and put it in a variable called underscore. Also name exports, where you can only import like a variable or a function from that package, in this case, shuffle. And name space exports, where you can import everything and make an alias to what you just imported. And if you have worked with React or with Angular, this way of importing, this should look very familiar. File extensions must be provided in two cases. When you're using the import keyword and also in directory indexes. So in those two cases, you always need to put the .js or the .jms extension at the end of the file. So, work in progress. There are two things that are expected to come soon in ECMAC script modules. One is loaders, which is an API that allows co-sum loaders for runtime transpilation and code instrumentation. I just know I sounded like super smart saying that, but I don't even know what that means. No, no, I'm kidding. I know what it means. So code instrumentation is used to measure the performance of your code and runtime transpilation. It's just translating the code in a way that can be read by the compiler. And also the second thing is like, do a command.js and xmas script module package. As a developer, you can use both the standard mmas script module and the do a command.js and the command.js, which is the traditional way you have used import and export. Now guess what? Guess. Now I didn't write a blog post this time, but I will. I wanna do it. I will put it in the notes or blog post. Now, for me, the biggest priority of all in JavaScript, there are like some things in JavaScript that it just doesn't make sense. Or it's like, this is real code. This is like a part of a code. Like what kind of magic is it? Like it's just, let me show you what I'm talking about. So the biggest priority is JavaScript itself. So here I'm using node version 12.13, which is like one of the things, the latest version. So for example, if I do like true, it's true, right? It's very common. But if we do true plus true, guess what this does? Two. It's like, wow, great. And if we do string two plus one, it's like, oh, what happened? No, this is not part of the talk. Yes, okay. So if we do like string two plus one is 21, but if we do like string two minus one is one, that makes a lot of sense. Also like three greater than two greater than one, it's false because of course, right? And also, for example, if we do like type of not a number, it's a number. This is great. And if we do like not a number instance of number, it's false. So none is a number, but it's not an instance of numbers. If you can't get it, I don't understand how that works. Also like, I am a string. It would be like an instance of string. False, hmm. So what is an instance of string? So how we can solve this is like this. Const my string equals new string. I am a string. And then if we do my string instance of string, that's true. That's how we can solve this. So if we also do like, it's nine and nine, that makes total sense, right? But if we do magic, that's super great of JavaScript. Also if we do like zero dot one plus zero dot two, this is equals to zero dot three because you know, I know basic math, but no, it's not, it's not, it's false. Because zero dot one plus zero dot two is equal to 16 digitals. I don't, 16 decimals, I don't know why. But actually I know it's something called I triple E standard seven, five, four, double precision floating point number. And that took me a while to remember, so I will say it again. I'll triple E, now I'm kidding. But that means that basically JavaScript kind of do basic math. That's another way of saying that. Also there is, I recently read that is something, a concept of a minus zero in JavaScript. So if you do like minus zero plus minus zero, that's minus zero, it's like minus zero. I don't, what is that? I don't know. Also, there is another way of doing functions. So if I do like let p equals constructor, and they may do something like p and then p and then console log priority creates a function that just brings priority. That's great. That's another way of saying this. New function console log priority. This is the same as this, but just don't do this because it's not a good practice. And those are like some educations of the language that, yeah, I love JavaScript, but it has some weird stuff there. Now to finish this talk, there is a quote that I really like from a brilliant mind and it says, life is just like a priority, full of jokes, funny, entertaining, sometimes crazy and ridiculous and most important, it gives us the opportunity to laugh. And laugh without laugh is a life we're living. And if you want to know what brilliant minds it is, it was me, yeah. It's important to be living ourselves, right? This is my Twitter, and this is notes or Twitter, you can follow me, I am a living party, and in notes or we publish everything that's happening in the Node.js ecosystem, so please follow us. And because we're in Montreal, let me practice my French very quickly. So, merci d'être venu à mon conférence, j'espère que vous avez aimé cette parodie. Merci beaucoup. Mucho gracias. Thank you.