ramda-optimize-prop

JavaScript performance comparison

Test case created by ku8ar

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  /* actual code */
  
  function _isPlaceholder(a) {
    return a != null &&
           typeof a === 'object' &&
           a['@@functional/placeholder'] === true;
  }
  
  function _curry1(fn) {
    return function f1(a) {
      if (arguments.length === 0 || _isPlaceholder(a)) {
        return f1;
      } else {
        return fn.apply(this, arguments);
      }
    };
  }
  
  function _curry2(fn) {
    return function f2(a, b) {
      switch (arguments.length) {
        case 0:
          return f2;
        case 1:
          return _isPlaceholder(a)
            ? f2
            : _curry1(function(_b) { return fn(a, _b); });
        default:
          return _isPlaceholder(a) && _isPlaceholder(b)
            ? f2
            : _isPlaceholder(a)
              ? _curry1(function(_a) { return fn(_a, b); })
              : _isPlaceholder(b)
                ? _curry1(function(_b) { return fn(a, _b); })
                : fn(a, b);
      }
    };
  }
  
  function _isString(x) {
    return Object.prototype.toString.call(x) === '[object String]';
  }
  
  var _isInteger = Number.isInteger
  
  var nth = _curry2(function nth(offset, list) {
    var idx = offset < 0 ? list.length + offset : offset;
    return _isString(list) ? list.charAt(idx) : list[idx];
  });
  
  var path = _curry2(function path(pathAr, obj) {
    return paths([pathAr], obj)[0];
  });
  
  var paths = _curry2(function paths(pathsArray, obj) {
    return pathsArray.map(function(paths) {
      var val = obj;
      var idx = 0;
      var p;
      while (idx < paths.length) {
        if (val == null) {
          return;
        }
        p = paths[idx];
        val = _isInteger(p) ? nth(p, val) : val[p];
        idx += 1;
      }
      return val;
    });
  });
  
  var prop = _curry2(function prop(p, obj) {
    return path([p], obj);
  });
  
  var propOptimized = _curry2(function prop(p, val) {
    if (val == null) {
      return;
    }
    return _isInteger(p) ? nth(p, val) : val[p];
  });
  

};
</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
optimized
propOptimized('a', {a: true})
pending…
actual
prop('a', {a: true})
pending…

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

0 Comments