7

enter image description here

I start my server, and refresh the page in a browser, which then takes >2s to load the JS resource. If I then reload the page in any browser, it loads quickly.

This is only happening the first request after the server has been started. I suppose it has something to do with it putting together the JS file the first time, and then after that it is cached on the server.

Can anything be done to cut down this time?

I have tried both with and without the production settings (gzip, minify etc).

Client code:

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
</script>

Server code:

var express = require('express'),
    expressServer = express.createServer(),
    socketServer = require('socket.io').listen(expressServer);

expressServer.listen(1337);
Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109

2 Answers2

3

There is currently a bug in socket.io that is causing this. Make sure you do NOT have this set and it should load MUCH faster:

io.set('browser client gzip', true);          // gzip the file

The first call to load socket.io.js will try to compress it and store it in memory. You will run into these bugs:

You can get some speed increase by using the minified version and allowing caching until this is fixed:

io.set('browser client minification', true);  // send minified client
io.set('browser client etag', true);          // apply etag caching logic based on version number
bendiy
  • 595
  • 1
  • 7
  • 15
  • I think they already fixed the gzip issue, cause I don't have a problem with it. Am I right? Or it only happens when you use gzip on Windows? Mine is Ubuntu 12.04 – Maziyar Oct 14 '13 at 01:29
0

Somehow, your jQuery library, which is more than half as big as the socket.io library, downloads 50x faster. Perhaps it was cached from before? Ultimately, the browser is just downloading a file.

Anyways, this fellow claims to have shrunk it.

Community
  • 1
  • 1
Yusuf X
  • 14,513
  • 5
  • 35
  • 47
  • Please see my clarification. :) – Markus Hedlund May 31 '12 at 08:05
  • Sorry, I don't understand what is your clarification. At the risk of unintended irony, could you, um, elucidate? Anyways, to speed up, you have three options: 1) make the socket.io library smaller, so as to reduce the download size 2) bundle the socket.io with another library, so as to reduce the number of downloads, 3) download from a faster server – Yusuf X May 31 '12 at 17:20
  • I start the server, load the page in Chrome and then in Firefox. Chrome=slow, Firefox=fast. I then restart the server, load the page in Firefox and then in Chrome. Chrome=fast, Firefox=slow. – Markus Hedlund Jun 01 '12 at 09:43
  • tl;dr: First request is always slow, consecutive requests are fast, even if the consecutive requests are with another browser. – Markus Hedlund Jun 01 '12 at 09:45
  • So someone somewhere along the way between server and computer is cacheing it. Unless preload is an option, there's not much you can do, except make the first load less slow by making it smaller. – Yusuf X Jun 01 '12 at 16:30