You measure the latency between sending a request, and getting an answer.
The command line program ping
uses an ICMP packet for that, you use an HTTP request.
An HTTP request needs to establish a TCP connect, sending the the request (HTTP is quite chatty), parsing the request, handling the request, sending the response, and parsing the response.
But the major problem is the handshake, which makes the request three times slower.
To get the real latency you cannot use time / 3
, because the connection could be kept alive, in which case there is no handshake.
To summarize: don't use HTTP/TCP to measure the latency.
Edit:
You can just connect to a closed port, like 3 or 4. In this case the connection attempt (TCP syn) gets rejected by the server. (Syn is the first part in a TCP handshake.)
Online demo: http://jsfiddle.net/wDz38/.