return vs no return

JavaScript performance comparison

Revision 12 of this test case created

Info

How much does returning a value slow a function down?

Preparation code

<script>
  function noReturn(err, callback) {
    if (err) {
      callback('error');
    } else {
      callback(null, 'value');
    }
  }
 
  function doesReturn(err, callback) {
    if (err) {
      return callback('error');
    }

    return callback(null, 'value');
  }

function doesOneReturn(err, callback) {
    if (err) {
      return callback('error');
    }

   callback(null, 'value');
  }

  function doesValuelessReturn(err, callback) {
    if (err) {
      callback('error');
      return;
    }
    callback(null, 'value');
  }

  function short(err, callback) {
    err && callback('error');
    callback(null, 'value');
  }

</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
noReturn
noReturn(null, function(err, result) {});
noReturn('err', function(err, result) {});
pending…
doesReturn
doesReturn(null, function(err, result) {});
doesReturn('err', function(err, result) {});
pending…
doesValuelessReturn
doesValuelessReturn(null, function(err, result) {});
doesValuelessReturn('err', function(err, result) {});
pending…
doesOneReturn
doesOneReturn(null, function(err, result) {});
doesOneReturn('err', function(err, result) {});
pending…
short
short(null, function(err, result) {});
short('err', function(err, result) {});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment