nanha
12/6/2011 - 12:23 AM

Node.js performance tips

Node.js performance tips

// Good: write files asynchronously
fs.writeFile('message.txt', 'Hello Node', function (err) {
  console.log("It's saved and the server remains responsive!");
});

// BAD: write files synchronously
fs.writeFileSync('message.txt', 'Hello Node');
console.log("It's saved, but you just blocked ALL requests!");
<!-- An example of a simple webpage rendered entirely server side -->

<!DOCTYPE html>
<html>
  <head>
    <title>LinkedIn Mobile</title>
  </head>
  <body>
    <div class="header">
      <img src="http://mobile-cdn.linkedin.com/images/linkedin.png" alt="LinkedIn"/>
    </div>
    <div class="body">
      Hello John!
    </div>
  </body>
</html>
<!-- An example of a JavaScript template that can be rendered client side -->

<!DOCTYPE html>
<html>
  <head>
    <title>LinkedIn Mobile</title>
  </head>
  <body>
    <div class="header">
      <img src="http://mobile-cdn.linkedin.com/images/linkedin.png" alt="LinkedIn"/>
    </div>
    <div class="body">
      Hello <%= name %>!
    </div>
  </body>
</html>
app.use(express.session({ secret: "keyboard cat" }));
// Disable socket pooling

var http = require('http');
var options = {.....};
options.agent = false;
var req = http.request(options)
// Use built in or binary modules
var crypto = require('crypto');
var hash = crypto.createHmac("sha1",key).update(signatureBase).digest("base64");