Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / XML

Tick, Tock… Time Waits for No One

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
6 Jan 2014MIT1 min read 5.7K   1  
Time does not wait for anyone

Ok, so what I am about to discuss might be old news for most, but for me, it’s new. After discovering this, I have to admit that I am a bit disappointed in myself for not already knowing this.

When implementing asynchronous methods in Node, I had always done the following:

JavaScript
function asynchProcessFunc = function(callback) {
    setTimeout(function(callback){
        // process the request now
        callback(returnData);
    }, 0);

    // return back 
};

It was recommended by someone that I change this out for the process.nextTick(cb) method. Being a firmware developer in previous lifetimes, the pure mention of tick had me intrigued.

Was it actually processing CPU ticks?

Turns out the answer was No, but still what I did find was something far better than a zero based timeout. I had always known that setTimeout(cb, timer) with a zero based timer was never going to be accurate, but I had decided it was the best I could ever get.

To get an idea of a difference between the two, I wrote the following to measure setTimeout:

JavaScript
setTimeout(function(){
    var startTime = process.hrtime();

    setTimeout(function() {
        var timeDiff = process.hrtime(startTime);
        console.log('setTimeout time diff - ', timeDiff[0]*1e9 + timeDiff[1], '(nanoSecs)');
    }, 0);
}, 1000);

I rounded the average rate to 1600000.

I then changed the program to use process.nextTick() like the following:

JavaScript
setTimeout(function(){
    var startTime = process.hrtime();

    process.nextTick(function() {
        var timeDiff = process.hrtime(startTime);
        console.log('nextTick time diff - ', timeDiff[0]*1e9 + timeDiff[1], '(nanoSecs)');
    });	
}, 1000);

I rounded the average rate to 400000, which gives me a difference of about 4x performance increase. Depending on your product, this I am sure will change but even the improvement comes in at 2x that is still a major performance increase.

Happy coding everyone.

This article was originally posted at http://dniswhite.com?p=155

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
United States United States
I am software developer with over 20 years of professional experience. I have been employed as a software developer since the early 90′s back when Microsoft’s Windows 3.1x was gaining popularity and IBM’s OS/2 was the predominant leader in 32-bit PC based Operating Systems.

Prior to choosing this as my profession I had studied architecture and then later Electrical and Mechanical engineering in college. As a young kid growing up I always played with computers, my first computer was a TRS-80 that I would spend countless hours writing programs for, I never really thought of programming as a profession. The story goes that in my final year of college I took a C/C++ programming class and had so much fun working on the various projects that my professor told me something that changed everything.

“You know they pay people to do stuff like this for a living?” – Professor Bolman

Check out my blog here.

My current and ever evolving projects:

jqAlert javascript alerts done right for those using jQueryUI.
DooScrib Doodle and scribble pad written in javascript for use with HTML5 Canvas.

Comments and Discussions

 
-- There are no messages in this forum --