2

Consider the following functions where I am returning a value.

Method 1:

function getData() {
    var myData = '';
    var DateObj = new Date();
    for (i=0; i<50000; i++) {
         myData += "'" + DateObj.toString() +"', ";
    }

    return myData;
}

Method 2:

function getData(calbck) {
  try{
    var myData = '';
    var DateObj = new Date();
    for (i=0; i<50000; i++) {
         myData += "'" + DateObj.toString() +"', ";
    }
  } catch(e){
  return calbck(e,null);
  } 
  return calbck(null, myData);
}

I have tested both the ways works. Which on is the best to choose?

Thanks


Update: Tested with the following code.

function getData1() {
    var myData = '';
    var DateObj = new Date();
    for (i=0; i<50000; i++) {
         myData += "'" + DateObj.toString() +"', ";
    }
    return myData;
}

function getData2(calbck) {
  try{
    var myData = '';
    var DateObj = new Date();
    for (i=0; i<50000; i++) {
         myData += "'" + DateObj.toString() +"', ";
    }
  } catch(e){
  return calbck(e,null);
  } 
  return calbck(null, myData);
}

console.log('call getData1() start');
console.time('5000000-getData1()');
getData1()
console.log('call getData1() End');
console.timeEnd('5000000-getData1()');

console.log('call getData2(calbck) start');
console.time('5000000-getData2(calbck)');
getData2(function(err,Returned_value){
if(err) return console.log(err);
   //console.log(Returned_value);
   console.log('call getData2(calbck) End');
   console.timeEnd('5000000-getData2(calbck)');
});

Output:

D:\Test>node app.js
call getData1() start
call getData1() End
5000000-getData1(): 91ms
call getData2(calbck) start
call getData2(calbck) End
5000000-getData2(calbck): 76ms

D:\Test>node app.js
call getData1() start
call getData1() End
5000000-getData1(): 111ms
call getData2(calbck) start
call getData2(calbck) End
5000000-getData2(calbck): 78ms
4
  • 3
    First is the best IMO, because it's simpler. There's no reason to use callbacks in this example because your function isn't asynchronous. Commented May 23, 2013 at 9:11
  • Updated the question with the test code Commented May 23, 2013 at 10:08
  • testing this based on performance is really moot. The difference (as @robertklep mentioned) is that the latter function with callback is used for async execution, for example when waiting for some IO-operation to complete. Commented May 23, 2013 at 10:12
  • Here's a gist which implements a truly (though crude) asynchronous version of the example code. Still, it's a contrived example and not to be used to benchmark synchronous versus asynchronous execution. Commented May 23, 2013 at 10:15

2 Answers 2

3

It all comes down to whether your function is synchronous or asynchronous.

If your function is synchronous, and all the calculation is done immediately, use the first method.

If your function asynchronous, and it uses resources such as disk or network I/O, or ends up waiting on anything, then the second method is really the only option.

In your example, the function is synchronous, so you should use the first one.

Sign up to request clarification or add additional context in comments.

Comments

2

There's good reason to go use the "asynchronous" function signature, even if its current implementation is synchronous. A function named getData (and especially its implementation, which screams "stub" to me) suggests that this function may be doing I/O (network, file system) in the future. If you give the getData function an asynchronous function signature now, you're forced to make all code using getData pass a callback. And that may be a a good thing!

Changing the function signature of such a method later on can have ramifications for all code based using getData, be it directly or indirectly.

If the question would be about a hypothetical computeSum function, and what method signature to give that, I'd definitely recommend to use a synchronous function signature. But now, it's not so clear.

In general, I'd give any function I write an asynchronous signature (or have it return a promise) if I see a good chance that its implementation will do I/O in the future, even if its current implementation does not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.