Click here to Skip to main content
13,139,515 members (50,344 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 6 Jan 2014

Tick, Tock… Time Waits for No One

, 6 Jan 2014
Rate this:
Please Sign up or sign in to vote.
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:

function asynchProcessFunc = function(callback) {
        // process the request now
    }, 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:

    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:

    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, along with any associated source code and files, is licensed under The MIT License


About the Author

Dennis E White
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.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 6 Jan 2014
Article Copyright 2014 by Dennis E White
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid