ds300 / derivablejs

Functional Reactive State for JavaScript and TypeScript
Apache License 2.0
515 stars 23 forks source link

Generalize implementation #73

Closed TrySound closed 6 years ago

TrySound commented 6 years ago

Right now or, mOr, and, mAnd implemented two times. This PR combines both implementation in one.

ds300 commented 6 years ago

Thanks! This is a neat idea, but I'm not sure it's worth the performance implications. Could you run some benchmarks to see if it has a big impact? I suspect it would.

TrySound commented 6 years ago

Well, I ran these benchmarks a few times. Similar results for all. Pointfree version somehow is executed a bit faster (hz)

  "combinators": {
    "fluent": {
      "hz": 422.0708803071039,
      "rme": 3.8206037068830545,
      "samples": 86
    },
    "pointfree": {
      "hz": 514.6142434777211,
      "rme": 1.2247664150120863,
      "samples": 88
    }
  },

combinators.pointfree.js

'use strict';
const djs = require('../../dist/derivable');

module.exports = function () {
  for (let i = 0; i < 100; i++) {
    const a = djs.atom(false);
    const b = djs.atom(true);
    djs.or(a, b).react(() => {});
  }

  for (let i = 0; i < 100; i++) {
    const a = djs.atom(false);
    const b = djs.atom(true);
    djs.mOr(a, b).react(() => {});
  }

  for (let i = 0; i < 100; i++) {
    const a = djs.atom(false);
    const b = djs.atom(true);
    djs.and(a, b).react(() => {});
  }

  for (let i = 0; i < 100; i++) {
    const a = djs.atom(false);
    const b = djs.atom(true);
    djs.mAnd(a, b).react(() => {});
  }
};

if (require.main === module) {
  module.exports();
}

combinators.fluent.js

'use strict';
const djs = require('../../dist/derivable');

module.exports = function () {
  for (let i = 0; i < 100; i++) {
    const a = djs.atom(false);
    const b = djs.atom(true);
    a.or(b).react(() => {});
  }

  for (let i = 0; i < 100; i++) {
    const a = djs.atom(false);
    const b = djs.atom(true);
    a.mOr(b).react(() => {});
  }

  for (let i = 0; i < 100; i++) {
    const a = djs.atom(false);
    const b = djs.atom(true);
    a.and(b).react(() => {});
  }

  for (let i = 0; i < 100; i++) {
    const a = djs.atom(false);
    const b = djs.atom(true);
    a.mAnd(b).react(() => {});
  }
};

if (require.main === module) {
  module.exports();
}