A Conversation About JavaScript: Node.js, Backbone.js, and Garbage Collection

Richard Campbell and Carl Franklin chat with developer Derick Bailey about the state of JavaScript development, covering topics including Node.js and server-side JavaScript, the benefits of Backbone.js, and garbage collection in JavaScript -- with a brief look at JavaScript and Windows 8.

Richard Campbell

December 2, 2012

10 Min Read
A Conversation About JavaScript: Node.js, Backbone.js, and Garbage Collection

RELATED: "How to Structure JavaScript Code, Part 1" and "Enabling a Node.js Server-Side App on Windows Azure."

Editor's note: Welcome to .NETRocks Conversations, excerpts from the .NET Rocks! weekly Internet audio talk show. This month's excerpt is from show743, with Derick Bailey, an independent software developer, consultant, trainer, and blogger who works with JavaScript, ASP.NET MVC, and other software tools andtechnologies. Derick joins Carl and Richard in a lively conversation about JavaScript development, discussing Node.js, Backbone.js, JavaScript garbagecollection, and JavaScript and Windows 8.

Carl Franklin:JavaScriptcertainly has come a long way. It seems to get better and better and faster and faster. What are you seeing out there in the JavaScript world as being thelatest trend?

Derick Bailey:Well, the latest trend is definitely Node.js.

Richard Campbell:It is hip, isn't it?

DB:It has become the darling framework of the JavaScript world and the web development community in general, recently.

CF:Who knew JavaScript programmers could write server apps?

DB:Yeah, I know. It’s crazy.

CF:So, is Node.js, you think, sparking a new interest for the JavaScript world in more traditional, sort of plumbing, back-end programming outside of Node.js?Or do you think that it's confined to Node.js at the moment?

DB:It's a vibrant and thriving growth. Node.js kind of stepped in at the beginning of this wave that we're seeing and helped push it along, but it's really areciprocal, cyclical process where all of these other technologies are beginning to use JavaScript. Node is beginning to pick up additional libraries andtools to connect to these other environments, and you're seeing this outgrowth in this very homogenous organic manner of all these tools starting to useJavaScript and the JSON format in order to get things done.

CF:Right. Do you think there's a lot more JavaScript programmers doing stuff on the server now, or do you think that it's server programmers now using WinJS?Like people who have done things typically in PHP or even ASP.NET or Java on the server are now thinking, "Well, let's try this."

DB:My best guess would be about 50/50, honestly. I think a lot of developers who were previously doing back-end stuff are now saying, "This JavaScript,there's actually some cool stuff that I can do with it as a back-end developer."

And then, at the same time, people like me who, you know I've spent a lot of years in the back end, but I always had this JavaScript front-end work that Ihad been doing. I saw Node come along and these other tools, and I said, "Oh, hey. I can take JavaScript from the front end and start doing it in the backend now. This is pretty cool." This bridges the gap in terms of skill sets, so we're seeing kind of a convergence finally between the front-end developerand the back-end developer with tools like Node.

CF:Is there any march toward standardization or development of practices and things, or is that too much to ask of the JavaScript community at this point?

DB:Oh, it's definitely not too much to ask. There has been a huge push for that recently. There's a number of books that I've read recently in the last yearor two that really start heading down that path quite well. One of my favorites -- actually probably my all-time favorite JavaScript book -- is by a guynamed Stoyan Stefanov. The book is called JavaScript Patterns, and it's all about good software engineering practices and good patterns forJavaScript to help create better code, more maintainable code, easier to read, high-performance, and things like that.

RC:There's a guy I've also been following. Just a really good thinker about how to write code efficiently. I don't even look at Stoyan as a guy all aboutJavaScript. He's a really good thinker about programming.

DB:Yeah, absolutely he is. He does a lot of work in PHP and other languages, too.

CF:So, are there any other gurus out there that people are looking to for guidance, even architecturally or pattern development-wise?

DB:There are definitely a good number of JavaScript luminaries who are really pushing things forward and saying, "Look, these things need to be done better.We need to think about architecture and performance and writing good, maintainable software because our language is becoming ubiquitous, and it's really anecessary part of the growth."

CF:Yeah, I just wonder if JavaScript programmers are wondering about optimizing memory management and things like that. Those are things that have beencompletely foreign to the JavaScript developer. Are you thinking about those kinds of things with Node.js?

DB:Not necessarily with Node. I'm not doing a lot of Node work right now. I'm mostly just playing around with it and learning it, but when I'm doingbrowser-based JavaScript I am absolutely thinking about memory management.

RC:Hmmm.

DB:Every time I implement an object, for example, I'm thinking about, "OK, what attributes and methods do I need to stick in the constructor function versusin the object prototype?" And a lot of that has to do with memory management.

We also have to think about things like closures and variable scope and making sure that we don't just have closures left and right running amok becausethe closure almost by definition is a memory leak. So we really have to control those things and make sure that we're keeping everything well encapsulatedand closed down tight, so that we don’t create real memory leaks.

CF:It's funny just hearing the words "JavaScript" and "memory leak" in the same sentence, but I mean, think about it, when you're doing an asynchronousJavaScript application, [it] is really a stateful being, isn't it?

DB:Oh, yeah. Absolutely.

CF:That some guys are going to sit on maybe for hours and use.

DB:Right.

RC:But this brings up an interesting question, Derick, because I think that the sophistication of JavaScript and these great frameworks have changed the waywe build web pages entirely.

DB:Absolutely.

RC:This whole idea that there is only one page, there's like the single page, and you just keep Ajaxing different content into it, which, to me, feels likethe ultimate memory leak.

DB:Well, it's going to be close. You can definitely get down that path, but JavaScript is a garbage-collected, managed language much like .NET or Ruby orother modern languages that we use. So it really behooves a developer to understand how and when some of the basic garbage collection happens inJavaScript. Then you really avoid memory leaks by allowing your objects to be collected when they need to be.

CF:Now, how does the garbage collection work in JavaScript, and is it the same for each browser and each engine? Is there a standard garbage collector?

DB:I don't think there is at this point. I'm pretty sure that each of the browsers does it on their own. There might be some standard in the JavaScript, theECMAScript standard, but I don't know offhand. My assumption based on some blog posts and other information that I've read recently is that JavaScriptgarbage collection works much the same way as .NET garbage collection in that it looks for objects that are still in use and still referenced by theapplication, by the DOM, or by some other means, to say "hey, there is a possibility that this object is still going to be used." And anytime it sees that,it hangs onto the object.

There's a couple of ways that you can force an object to be cleaned up. You can either de-reference it everywhere, just let it fall out of scope, and atsome point it will be cleaned up, or you can explicitly call the delete keyword, and the delete keyword essentially says "remove areference to whatever object this variable or attribute was pointing to." And, of course, I'm over-simplifying everything greatly; there's a lot moretechnical details behind the scenes, but that's the gist of it.

RC:That brings up this whole idea: If you're just going to stay on one page, you're now responsible for cleaning that stuff up. And how much have we countedon the memory cleanup effects of going to another page?

DB:Right.

CF:Backbone.js. What can you tell us about that?

DB:I'm a huge fan of Backbone. It's actually where the vast majority of my paid work is these days, and my open source projects at that. Backbone is, contraryto popular opinion, not a JavaScript MVC framework. It's actually more of a library of tools that happens to fit into the MV* family, but it's really notMVC.

Number one, there's no controller, but also, philosophically and architecturally, it just doesn't fit into that paradigm where you have controllers thatare being called by routers that end up using your models and that kind of frameworky "I'm going to call your code" manner. Backbone is really more of a"hey, here's some great abstractions that have come out of a lot of different ideas and a lot of different experience, and you can go and reuse theseabstractions in whatever manner you want." It's just a library of tools.

CF:What's a typical place where this sits in your web stack? Give me an example of an application that would benefit really well from using it.

DB:The most simple place where Backbone is beneficial is anytime you see jQuery code that's having more than a few nested callbacks and more than, I don'tknow, 20 or 30 lines of code. The Backbone view construct is really there to help us organize and clean up our jQuery code. Of course, it can do a lot morethan that when we want it to, but that's most often the first place that I introduce Backbone into a project.

RC:Is when jQuery gets out of hand?

DB:Yeah, yeah. I want to bring some better structure to this jQuery code, so I'm going to bring Backbone into play and start using Backbone to organize thatjQuery code.

RC:Interesting.

CF:So, Windows 8 and Metro. Microsoft really did a good thing for JavaScript developers, do you think?

DB:Oh, yeah. Absolutely. I had a chance to play with the Microsoft BUILD Samsung tablet from last year I think it was, or the year before. I really enjoyedthat Windows 8/Metro-style tablet experience. It was very unique, but it was also very intuitive. They had some really cool things going on with it, and Iwas happy to hear that Microsoft was doing the whole HTML5/CSS/JavaScript thing in order to be able to build apps on top of Metro.

CF:Well, here's the big question. Do you personally know anybody who wasn't in the Microsoft camp before and who was doing JavaScript, CSS, HTML5 programmingand, because of that, is getting into and excited about Metro?

DB:I don't think I could name any names specifically, but I do have some vague memories and some specific memories of people on Twitter saying, "You knowwhat? This Windows Phone 7 thing is really nice. I like this user interface." And I'm not sure about Windows 8 specifically, but I can't imagine that itwouldn't be happening a little bit more and a little bit more because quite frankly it is a really nice experience. And the ability to write JavaScripthits the hardware level in order to do some really advanced stuff; it's going to be a lot of fun building apps for Windows 8 tablets.

RC:I'm just wondering how much the fact that it's compiled, the customizations for the Chakra engine with the contracts and so forth, are just going to makeit such a unique development environment that the skills don't really cross over.

DB:I think we're starting to see some of that fragmentation in the JavaScript world already, honestly. And I'm not sure it's going to be that much of aproblem. I think it falls along the lines of C++. You can go write C++ for Windows or for UNIX or for whatever operating system. It's the language andsyntax and the general knowledge and experience that you transfer with you, but then you're going to have to learn the specifics of that operating system'sAPIs and how to really deal with the kernel in that environment.

There's much more! You can find the full interview at dotnetrocks.com/default.aspx?showNum=743.

Richard Campbelland Carl Franklin are the voices and brains behind .NET Rocks!. They interview experts to bring you insights into .NET technology and thestate of software development.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like