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


what's best way declare variables when requiring modules in nodejs? different well-known node developers follow different styles. example, tj holowaychuk uses style:

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

ryan dahl, on other hand, prefers way:

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

note: question not regarding style (which has been covered extensively in this gist), rather of 2 methods produce optimized code (bonus points if specific case of requiring modules). understand difference won't much, believe worth knowing. example, in c++, this question comes conclusion build process benefit little smaller number of characters parse. apart that, there other advantage of 1 method on other?

...which of 2 methods produce optimized code...

i'd surprised if there difference in actual machine code v8 produces when compiles source (other debug info). can't believe there's difference has real-world impact difference in character count, , fact 1 produces single statement using comma operator join multiple expressions , other produces series of statements (one expression/statement). style thing.

for this:

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

the engine processes this:

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

...e.g., declarations followed single statement made of 3 assignment expressions joined comma operator.

for this:

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

the engine processes this:

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

...e.g., declarations followed 3 statements 1 assignment expression each.

in both cases, declaration step removed initialization, assignment. upon entry context, variables declared var (and function declarations) create entries in binding object of lexical environment of execution context, prior step-by-step code being run. why they're said "hoisted," , why styles above largely irrelevant engine. (more: poor misunderstood var)

and in terms of difference in resulting statement(s) once declarations factored out, doubt there's real difference in machine code.


Comments