Monthly Archives: February 2018

Properly measuring HTTP request time with node.js

When your backend code is calling external APIs you may want to measure particular request time to identify bottlenecks.

The most straight forward, but incorrect, way to measure how long request takes is to use JavaScript Date object:

var request = require('request');

let start_time = new Date().getTime();

request.get('https://google.com', function (err, response) {
    console.log('Time elapsed:', new Date().getTime() - start_time);
});

However, this won’t give you the actual time that request takes. Above request call is async, and you start measuring time at the time when request was queued, not actually sent.

In order to determine how much time elapsed since sending request, you can use the time parameter:

var request = require('request');

request.get({ url: 'http://www.google.com', time: true }, function (err, response) {
    console.log('The actual time elapsed:', response.elapsedTime);
});

You can also compare results returned by both methods:

var request = require('request');

let start_time = new Date().getTime();

request.get('https://google.com', function (err, response) {
    console.log('Time elapsed since queuing the request:', new Date().getTime() - start_time);
});

request.get({ url: 'http://www.google.com', time: true }, function (err, response) {
    console.log('The actual time elapsed:', response.elapsedTime);
});

When I run it, I got the following results:

The actual time elapsed: 72
Time elapsed since queuing the request: 156

Notice that the first callback resolves after the second one(!)

The difference is almost 2x. Depending on your server side code, this difference might be even larger, and give you incorrect hints while you are profiling your application.


Boogie board – notepad of the future

Are you using paper notepads to write down ad-hoc notes?

These multi page paper notebooks are super useful. You can just turn the page, save your old sketch and have clean page for new one! WRONG! This is the worst feature! You never look at these notes again, and they just pile up.

Recently, I got Boogie Board – an LCD writing tablet! It cost $20 and it changed my life.

BoogieBoard

You can sketch whatever you want, and erase with one button click. It’s like a pocket whiteboard. If something is important I just dump it to my OneNote before erasing (rarely happens). You don’t have to look for pen anymore. You have one that can be attached to the board, and you can even write with your hands (nails) on it.

I also got bigger one for in-office use. My desk before and after:

BoogieBoard - before BoogieBoard - after

Get one or big one for yourself! It will change your life!