Artsiom Miksiuk
Artsiom Miksiuk

Reputation: 4323

Sync vs Immediate Async execution performance bench

I'm interested in performance comparison of synchronous and asynchronous data conversion test in case if async function would return result immediately. Will this affect on execution time?

To check this, I wrote simple bench to check.

const { performance } = require('perf_hooks');

const data = JSON.stringify((() => {
    const obj = {};
    for (let i = 0; i < 1000; i++) {
        obj[i] = i;
    }
    return obj;
})());

function convertSync (data) {
    return JSON.parse(data);
}

async function convertAsync (data) {
    return JSON.parse(data); 
}

const REPEAT_COUNT = 10000;

performance.mark("sync_start");
for (let i = 0; i < REPEAT_COUNT; i++) {
    convertSync(data);
}
performance.mark("sync_end");

performance.mark("async_start");
Promise.resolve()
    .then(async () => {
        for (let i = 0; i < REPEAT_COUNT; i++) {
            await convertAsync(data);
        }
        performance.mark("async_end");
    })
    .then(async () => {
        performance.measure("sync", "sync_start", "sync_end");
        performance.measure("async", "async_start", "async_end");

        console.log("REPEAT_COUNT", REPEAT_COUNT);
        console.log(performance.getEntries().filter(x => x.entryType === "measure"));
    }) 

I'm using Node v8.11.1 without any transpilers and getting following results.

PS D:\TestProjects\PerfTest\promisevssync> node .\index.js
REPEAT_COUNT 100000
[ PerformanceEntry {
    duration: 7825.5724,
    startTime: 383379071.208899,
    entryType: 'measure',
    name: 'sync' },
  PerformanceEntry {
    duration: 7836.966301,
    startTime: 383386896.834899,
    entryType: 'measure',
    name: 'async' } ]
PS D:\TestProjects\PerfTest\promisevssync> node .\index.js
REPEAT_COUNT 10000
[ PerformanceEntry {
    duration: 788.824201,
    startTime: 383405055.485299,
    entryType: 'measure',
    name: 'sync' },
  PerformanceEntry {
    duration: 798.591301,
    startTime: 383405844.370999,
    entryType: 'measure',
    name: 'async' } ]

As you can see execution time is the same. For higher repeat count numbers are still not differs much, so I believe that this is just a nose. Such results raises two questions:

  1. Is this test correct?
  2. Why?

I understand that I missing something, but it seems that I forgot to take some fundamental thing in to account.

Upvotes: 0

Views: 197

Answers (2)

Estus Flask
Estus Flask

Reputation: 223318

I'm interested in performance comparison of synchronous and asynchronous data conversion test in case if async function would return result immediately. Will this affect on execution time?

The measurements are correct but notice that what's measured is the performance of synchronous and asynchronous iteration, while data conversion stays synchronous.

Node.js has high-resolution timer, so promises themselves introduce only a slight lag that is negligible in comparison with time spent on convertAsync call.

This test with 10000 iterations confirms this:

for (let i = 0; i < REPEAT_COUNT; i++) {}

vs

for (let i = 0; i < REPEAT_COUNT; i++) {
  await null;
}

The measurements are 0.5ms for synchronous loop vs 9ms for asynchronous.

In contrast with a dry run, most of the time will be spent on JSON.parse(data).

Upvotes: 2

Shubham Gupta
Shubham Gupta

Reputation: 2646

For async-await to work your function should return a promise which is not the case. So you are getting similar results since convertAsync is not truly async function. Internally what javascript does is wrap your function in resolved promise and since your promise is almost immediately resolved you see minimal diff in your results.

Take a look at https://javascript.info/async-await

Upvotes: 2

Related Questions