Macros vs Functions

JavaScript performance comparison

Test case created by Charles Pick

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.8.0/lodash.min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  function macroDemo() {
    var _input = [1, 2, 3, 4];
  
    var _visitor = function _visitor(item) {
      return item + 1;
    };
  
    var _map = undefined;
  
    var _length = _input.length;
    var _result = new Array(_length);
    for (var _i2 = 0; _i2 < _length; _i2++) {
      _result[_i2] = _visitor(_input[_i2], _i2, _input);
    }
    _map = _result;
  
    return _map;
  }
  
  var _macrovisitor = function _visitor(item) {
      return item + 1;
  };
  
  function macroDemoNoClosure() {
    var _input = [1, 2, 3, 4];
  
  
    var _map = undefined;
  
    var _length = _input.length;
    var _result = new Array(_length);
    for (var _i2 = 0; _i2 < _length; _i2++) {
      _result[_i2] = _macrovisitor(_input[_i2], _i2, _input);
    }
    _map = _result;
  
    return _map;
  }
  
  function fnDemo () {
    return [1, 2, 3, 4].map(function (item) { return item + 1; });
  }
  
  function lodashDemo () {
    return _.map([1, 2, 3, 4], function (item) { return item + 1; });
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Native Functions
if (fnDemo().length !== 4) { die(); }
pending…
Macros
if (macroDemo().length !== 4) { die(); }
pending…
Lodash
if (lodashDemo().length !== 4) { die(); }
pending…
Macros (No Closure)
if (macroDemoNoClosure().length !== 4) { die(); }
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.

0 Comments