'Simple practical example to see faster async functions and promises from node 10 to node 12, and up

Before I ask my question I want to make this clearly, I know benchmarks never tell the whole story. But my goal here is relatively simple, I want to see the performance improvement from node 10 to node 12 (and up) regarding to async functions and promises. I just need a rough idea to see how much improvement I can get, after all that is what here said https://v8.dev/blog/fast-async

So I try hard to find a simple code to do the test, then I find this from https://fibjs.org/en/docs/guide/about.md.html, which is far more easier than other test codes I can come up with.

var count = 1000;

async function test_async(n) {
    if (n == count)
        return;
    await test_async(n + 1);
}

function test_callback(n, cb) {
    if (n == count)
        return cb();

    test_callback(n + 1, cb);
}

function test_sync(n) {
    if (n == count)
        return;
    test_sync(n + 1);
}

async function test() {
    console.time("async");
    await test_async(0);
    console.timeEnd("async");

    console.time("callback");
    test_callback(0, () => {
        console.timeEnd("callback");
    });

    console.time("sync");
    test_sync(0);
    console.timeEnd("sync");
}

test(); //actually I also add a python timeit like code to repeat the test()

Running this code many times I do find node 12 shows many improvements compared to node 10. Unfortunately I don't see many improvement from node 12 to node 14.

My question is is this test code effective in verifying the improvement of async function ?

---- update for node 16 ----

nodejs.16 was released in 2021.4.21 but to my surprise when I run using my script with node 16 its performance was even worse than nodejs 12!

As node16 V8 upgraded to V8 9.0 I had thought I should get some performance improvement. Not sure why

---- update again ----

If I just run my test once, node 16 indeed performs best. But if I run the test more than once, say 10 or 15 time, node 16 becomes worst.

I initially use some home-made timeit library to run the test, to reduce any unexpected factor, now I just run test like following.

async function test() {
    //async
    console.time('async')
    for (let index = 0; index < 15; index++) {
        await test_async(0)
    }
    console.timeEnd('async')
    //sync
    console.time('sync')
    for (let index = 0; index < 15; index++) {
        test_sync(0)
    }
    console.timeEnd('sync')
} 

Node 16 always performed worst.



Solution 1:[1]

I notice node18 was released and V8 JavaScript engine was upgraded to 10.1 so I did the test again and found node18 has significantly improved the test result, almost reduced the 50% of running time of node 14.

Actually although in my initial test node 16.0 was slower than nodejs 14.15, I find node 16.14.2 has performed better than node 14.19.0.

Then on the other hand I do feel test used by fibjs is a good one.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Qiulang