background preloader

Node.js

Facebook Twitter

POCs

Distributed. Hosting. Libs. Paris events. Tutorials. Node.js. Using event capturing to improve Basecamp page load times. We’ve been busy tightening the nuts and bolts on the all-new Basecamp in the wake of last week’s launch.

Using event capturing to improve Basecamp page load times

As part of the process, I decided to take a closer look at client-side page load performance. Shaving even a tenth of a second off of page load time can have a big impact on perceived performance, so it’s a worthwhile investment. Profiling the JavaScript page load event led to a surprising revelation. On pages with many to-dos, an overwhelming majority of the time was spent initializing sortable behavior. The culprit looked something like this: $.ready(function() { $("article.todolist, section.todolists").sortable(); }); In other words, when you’d load any page with to-dos on it, Basecamp would make the items of each to-do list reorderable, then make the whole collection of to-do lists reorderable.

Here’s the thing: you probably aren’t reordering to-dos every time you visit a project. Deferring Initialization That got me thinking. Sadly, this code doesn’t work. Why node.js streams are awesome. Tl;dr: You should compose your web app with IO streams On dump.ly , one of our most loved features is the download button, which creates a zip file with all the original images in an album.

Why node.js streams are awesome

The original solution was hacked up very quickly (ie was pretty ghetto). It simply downloaded each file from S3 into a temp folder on the server, zipped up that folder via a shell exec, and then sent the resulting archive down the pipe. While it worked correctly, there were some problems: Initial latency The user has to wait while each file is downloaded to the server and zipped up. Spiky Server Load The S3 downloads and zipping are both spiky CPU and IO intensive. Complexity While this was supposed to be the quick and simple solution, it very quickly got messy as we needed to ensure all temporary files got deleted in case of errors. Repeated work Each and every request would generate the whole zip file again, even if the download was aborted. Node.js. Erlang (programming language) The first version was developed by Joe Armstrong in 1986.[5] It was originally a proprietary language within Ericsson, but was released as open source in 1998.

Erlang (programming language)

The name "Erlang", attributed to Bjarne Däcker, has been understood[by whom?] As a reference to Danish mathematician and engineer Agner Krarup Erlang, and (initially at least) simultaneously as a syllabic abbreviation of "Ericsson Language".[5][6] In 2006, native symmetric multiprocessing support was added to the runtime system and virtual machine.[5] Quoting[9] Mike Williams, one of the three inventors of Erlang:[10] Find the right methods—Design by PrototypingIt is not good enough to have ideas, you must also be able to implement them and know [how|that] they workMake mistakes on a small scale, not in a production project A factorial algorithm implemented in Erlang: A sorting algorithm (similar to quicksort): The above example recursively invokes the function qsort until nothing remains to be sorted.

Event loop. When the event loop forms the central control flow construct of a program, as it often does, it may be termed the main loop or main event loop.

Event loop

This title is appropriate because such an event loop is at the highest level of control within the program. Message passing[edit] Alternative designs[edit] This approach is in contrast to a number of other alternatives: Traditionally, a program simply ran once then terminated. Usage[edit] function main initialize() while message ! File interface[edit] For example, consider a program that reads from a continuously updated file and displays its contents in the X Window System, which communicates with clients over a socket (either Unix domain or Berkeley):

Just-in-time compilation. Applications[edit] A common implementation of JIT compilation is to first have AOT compilation to bytecode (virtual machine code), known as bytecode compilation, and then have JIT compilation to machine code (dynamic compilation), rather than interpretation of the bytecode.

Just-in-time compilation

This improves the runtime performance compared to interpretation, at the cost of lag due to compilation. JIT compilers translate continuously, as with interpreters, but caching of compiled code minimizes lag on future execution of the same code during a given run. Since only part of the program is compiled, there is significantly less lag than if the entire program were compiled prior to execution.

Overview[edit] Node.js: JavaScript on the Server. Jade - Template Engine. Connect - High quality middleware for node.js. Connect is a middleware framework for node,shipping with over 18 bundled middleware and a rich selection of3rd-party middleware. var app = connect() .use(connect.logger('dev')) .use(connect.static('public')) .use(function(req, res){ res.end('hello world\n'); }) http.createServer(app).listen(3000); Installation: $ npm install connect.

Connect - High quality middleware for node.js