1

I have some functions that do a lot of calculations with data and arrays but all in memory, it does not do any IO. I've been reading trying to get into nodejs, I was wondering if there is any benefit on making then async? For example, function a:

function a(b, c, d) {
  // a lot of operations with arrays
  return value;
}

Does it make any sense to return a promise in this case if it does not involve any IO blocking operation? For example:

function a(b, c, d) {
  return new Promise(resolve, reject) {
    // a lot of operations with arrays
    resolve(value);
  }
}

Is there any improvement in using promises in this case or am I only overcomplicating this and should just return the value and not a promise?

MauricioRobayo
  • 2,207
  • 23
  • 26
  • 2
    the executor passed to the Promise constructor is executed synchronously - so, no benefit (though, curiously, I've seen some code run 10 times faster - at least in firefox, so not that relevant I guess - inside the executor) - note: the Promise code you've posted is invalid – Jaromanda X Mar 25 '18 at 23:21
  • 1
    Perhaps this thread can help you out: https://stackoverflow.com/questions/43482775/single-thread-synchronous-and-asynchronous-confusion?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – John Knoop Mar 25 '18 at 23:32
  • 1
    Having said that, you can write the code for `// a lot of operations with arrays` in such a way that it doesn't "block" - i.e. make it pseudo-asynchronous, by performing small batches of operations in a controlled sequence of setTimeout/setImmediate/whatever is available - and in that case a Promise could be beneficial. It really depends on what the "operations" are, and why you would want them to be "non blocking" - (is this part of some type of server, and it stops responding to client requests while intensive calculations are being performed?) – Jaromanda X Mar 25 '18 at 23:36
  • @JaromandaX Yes, they are part of a server process before giving a response, at this point I am not getting any performance issues but I am afraid that at some point with more request I will begin to have problems. – MauricioRobayo Mar 25 '18 at 23:43
  • 1
    Check out my answer - obviously I can't help with how you "split" and perform the "some operations on arrays" as you haven't shown any detail about the "lot of operations with arrays" - but hopefully that will give you an idea of what can be done – Jaromanda X Mar 25 '18 at 23:58

2 Answers2

4

No, async functions are only syntax sugar on top of promises. If your function doesn't actually have any asynchronous operations (like a network request), it will keep executing synchronously and will resolve synchronously, so it'll be no different from an ordinary function.

Javascript is single-threaded.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • 2
    This is only 99% true. All `.then()` handlers execute asynchronously so putting a bunch of synchronous code inside an `async` function or inside a regular function that returns a promise will resolve the promise immediately, but won't synchronously call the `.then()` handlers - they will still be called on a future tick. In any case, one should not use an `async` function for entirely synchronous code. – jfriend00 Mar 25 '18 at 23:38
  • 1
    I know the word "async" was used and the question tagged with `async-await` - but the question really has nothing to do with what `async-await` is, the OP doesn't even use `async` or `await` keywords in the code!! Not sure how this could possibly answer the question :p – Jaromanda X Mar 25 '18 at 23:38
  • @JaromandaX I use `async - await` to call this functions inside another functions. Unless there is another answer in some other directions, this answer does solve my question as now I know that I am not getting any benefit from returning a promise instead of just returning the value. – MauricioRobayo Mar 25 '18 at 23:46
1

You can do the lot of operations with arrays in "batches" to avoid "blocking" other execution - the following is "pseudo" code in that you haven't shown what // a lot of operations with arrays actually is ... so

function a(b, c, d) {
    return new Promise((resolve, reject) => {
        function processBatch() {
            // do some operations with arrays
            if (error) {
                reject(error);
            } else if (needToProcessMore) {
                setImmediate(processBatch);
            } else {
                resolve(someResult);
            }
        }
        setImmediate(processBatch);
    });
}

Alternatively, (though I've never used them myself) there are "thread" and "thread worker" type libraries available for node

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87