Digging Into Node.js

Processing File Contents

Kyle Simpson

Kyle Simpson

You Don't Know JS
Digging Into Node.js

Check out a free preview of the full Digging Into Node.js course

The "Processing File Contents" Lesson is part of the full, Digging Into Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Kyle walks through processing a file's contents by converting a binary buffer into a string, making the characters uppercase, and writing the contents to standard output. The efficiency of the operation is then considered.

Preview
Close

Transcript from the "Processing File Contents" Lesson

[00:00:00]
>> Kyle Simpson: Now, dumping the contents of a file is not particularly interesting. We wanna do some more stuff with it. So let's consider just a very simple thing that we could do, which is that whatever the contents are of the file, we're gonna uppercase all of those string characters.

[00:00:16]
We'll assume we're working with Text files, and we're gonna uppercase the characters. Now, contents here is a buffer, if you remember. And we can't really uppercase a binary buffer. We're gonna have to turn it into a string, so that we can do that uppercasing on it. So we're gonna have to say, contents = contents, turn it into a string, so we can Call.toString on it.

[00:00:41]
And then call toUpperCase.
>> Kyle Simpson: Let's try it again with this file. And we notice that it prints the hello world in uppercase text. By the way, there's another file in that directory if you just want to see it dumping a bunch of stuff. There's a file in that directory that's called lorem.txt which is one megabyte of lorem and some text.

[00:01:08]
So now we can dump one megabyte to our shell if we really want to go crazy.
>> Kyle Simpson: But let's talk about that for a moment because I just pulled in an entire megabyte as a binary buffer and then I turned that entire megabyte into a string. So I made a separate string representation of it.

[00:01:32]
And then I made a third representation of that megabyte. By turning it into the uppercase form and then I wrote that string out and there had to been at least one or two levels of translation to get it from the string back in the binary buffer so that it could go out to the pausing system.

[00:01:48]
That's not what we would consider, general speaking, to be very efficient processing of that. And that's some foreshadowing the stuff that will be talking about in later exercises. You need to be wary of, if it's just a small like 11 characters like hello world, not a big deal.

[00:02:03]
But if you're dealing with processing some giant files, maybe like a gigabyte file, you don't want to pull the whole thing into memory at once, you're going to want to be processing things a little bit at a time. And that's really where node starts to shine. We'll see in a little while the usage of stream operations to do that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now