public
Last active

Node.js performance tips

  • Download Gist
BinaryModules.js
JavaScript
1 2 3
// Use built in or binary modules
var crypto = require('crypto');
var hash = crypto.createHmac("sha1",key).update(signatureBase).digest("base64");
DisableSocketPooling.js
JavaScript
1 2 3 4 5 6
// Disable socket pooling
 
var http = require('http');
var options = {.....};
options.agent = false;
var req = http.request(options)
ExpressSession.js
JavaScript
1
app.use(express.session({ secret: "keyboard cat" }));
JavaScriptTemplate.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<!-- 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>
ReturnJustJson.js
JavaScript
1
{"name": "John"}
ServerSideRenderedHTML.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<!-- 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>
Synchronous.js
JavaScript
1 2 3 4 5 6 7 8
// 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!");

couldn't comment on the blog post, but I'm curious as to how the local storage stuff is applied. Why not just utilize the browser's cache? then you get cache invalidation etc for free, seems like a bit of needless duplication but I'm curious about that.

With the browser cache, there are still a number of requests sent out to check if a new version is available. Also, the browser cache is pretty small and you dont have control over its caching policy. This allows you to really control the cache and reduce the total number of requests.

well that depends how you cache, they wont always issue conditional gets. just seems a little weird to me I guess but thanks! was curious

For DisableSocketPooling, does that affect http.createServer() or in the case of express, app.listen()? I tried adding that to an express app and got:
Error: connect ECONNREFUSED
Any ideas?

that's for the http client

How to render HTML on client side and serve it with cdn?

@talentedmrjones: This is on the httpclient not on the server. This allows you to talk to the same host without using pooling
@kevbook: You can use underscore or dust or any js templating system to render on client and have the templates sitting in CDN

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.