verybadalloc
verybadalloc

Reputation: 5798

What's the most optimal way to define module variables in nodejs?

What's the best way to declare variables when requiring modules in nodejs? Different well-known node developers follow different styles. For example, TJ Holowaychuk uses this style:

(method1)
var connect = require('connect')
  , Router = require('./router')
  , methods = Router.methods.concat('del', 'all')
  , middleware = require('./middleware')
  , View = require('./view');

Ryan Dahl, on the other hand, prefers this way:

(method2)
var express = require('express');
var socketio = require('socket.io');
var bench = require('./bench');

NOTE: My question is not regarding style (which has been covered extensively in this gist), but rather about which of the two methods produce the most optimized code (Bonus points if specific to the case of requiring modules). I understand that the difference won't be much, but I do believe it is worth knowing. For example, in C++, this question comes to the conclusion that the build process will benefit a little from the smaller number of characters to parse. Apart from that, could there be any other advantage of one method over the other?

Upvotes: 0

Views: 269

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1073978

...which of the two methods produce the most optimized code...

I'd be surprised if there were a difference in the actual machine code V8 produces when it compiles the source (other than debug info). I can't believe there's any difference that has any real-world impact from the difference in character count, and the fact that the one produces a single statement using the comma operator to join multiple expressions and the other produces a series of statements (one expression/statement). It really is just a style thing.

For this:

// Style 1
var express = require('express')
  , socketio = require('socket.io')
  , bench = require('./bench');

The engine actually processes this:

// Style 1
var express, socketio, bench;
express = require('express'),
socketio = require('socket.io'),
bench = require('./bench');

...e.g., declarations followed by a single statement made up of three assignment expressions joined by the comma operator.

For this:

// Style 2
var express = require('express');
var socketio = require('socket.io');
var bench = require('./bench');

The engine actually processes this:

// Style 2
var express, socketio, bench;
express = require('express');
socketio = require('socket.io');
bench = require('./bench');

...e.g., the declarations followed by three statements with one assignment expression each.

In both cases, the declaration step is completely removed from the initialization, which is really assignment. Upon entry to a context, all variables declared with var (and all function declarations) create entries in the binding object of the lexical environment of the execution context, prior to any step-by-step code being run. This is why they're sometimes said to be "hoisted," and also why the styles above are largely irrelevant to the engine. (More: Poor misunderstood var)

And in terms of the difference in the resulting statement(s) once the declarations are factored out, I doubt there's any real difference in the machine code.

Upvotes: 3

Related Questions