return false vs no return

Benchmark created on


Description

Does returning false at the end of a function have a performance cost? Can we just rely on undefined being falsy?

EDIT: The results are surprising. It seems as though returning a value at all has a performance cost, although after conducting another test it appears this assumption is wrong.

Setup

var test_even_a = function (i) {
      if (i % 2 === 0) return true;
    };
    
    var test_even_b = function (i) {
      if (i % 2 === 0) return true;
      else return false;
    };
    
    var test_even_c = function (i) {
      return i % 2 === 0;
    };
    
    var test_function = function (fn) {
      for (var i = 0; i < 1000; i++) {
        fn(i);
      }
    };

Test runner

Ready to run.

Testing in
TestOps/sec
no return
test_function(test_even_a);
ready
return false
test_function(test_even_b);
ready
no if condition
test_function(test_even_c);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.