2

What's the difference between this code:

var restify              = require("restify")
  , clientModule         = require("./lib/domain/client.js")
  , server               = restify.createServer();

And this one:

var restify = require(restify);
var clientModule = require("./lib/domain/client.js");
var server = restify.createServer();

Is the former more efficient than the latter? Or is it just a matter of writing less code?

Bilthon
  • 2,455
  • 8
  • 35
  • 44
  • You can test performance at http://jsperf.com/, but my guess is the performance difference will be minimal if at all there. It is certainly not something worth worrying over if you're seeking performance optimizations. Beyond that, it's mainly a matter of code style preference. – ajp15243 Feb 07 '14 at 04:35

2 Answers2

2

Nothing except the single var is consistent with the default jsLint rules. JavaScript has a concept called var hoisting where vars defined anywhere inside a function are hoisted to the top of that function (their assignments aren't touched which can make it appear harmless in most cases). By getting in the habit of writing your vars at the top you let the reality of execution match the intention of the source. Using a single var is a handy way to enforce this (there should be only one car in any function and it should be the first line (after 'use strict' if you want to be extra carful)

Jason Sperske
  • 28,710
  • 8
  • 67
  • 121
0

No, they're both exactly the same. Some prefer doing all variable declarations at the top of the function, because that's where they effectively are due to hoisting. jsLint rules and the JS style guide for AirBNB prefer one var, but it's preference.

Tyler McGinnis
  • 33,715
  • 16
  • 71
  • 75