From ee7a3537d1430bcc7e9c4eef5a430bdcf7cfb6ec Mon Sep 17 00:00:00 2001 From: sergey-gerasimov Date: Thu, 24 Mar 2016 17:33:08 +0300 Subject: [PATCH] performance edge case. needs to be solved --- .../scenarios/computedDiamondUpdate.js | 110 ++++++++++++++++++ ...edDiamond1000UpdatesWith500Dependencies.js | 3 + 2 files changed, 113 insertions(+) create mode 100644 performance-tests/scenarios/computedDiamondUpdate.js create mode 100644 performance-tests/tests/computedDiamondUpdates/computedDiamond1000UpdatesWith500Dependencies.js diff --git a/performance-tests/scenarios/computedDiamondUpdate.js b/performance-tests/scenarios/computedDiamondUpdate.js new file mode 100644 index 0000000..32ed883 --- /dev/null +++ b/performance-tests/scenarios/computedDiamondUpdate.js @@ -0,0 +1,110 @@ +var Benchmark = require('benchmark'); +var itDepends = require('../../out/dist/it-depends.js'); +var ko = require('knockout'); + +module.exports = function(updatesCount, dependenciesCount) { + Benchmark.prototype.args = { + updatesCount: updatesCount, + dependenciesCount: dependenciesCount, + ko: ko, + itDepends: itDepends + }; + + Benchmark.prototype.setup = function() { + var updatesCount = this.args.updatesCount; + var dependenciesCount = this.args.dependenciesCount; + var ko = this.args.ko; + var itDepends = this.args.itDepends; + + var initialValue = -1; + + function _noop() { + return function() {}; + } + + function setupKnockout() { + var x = []; + for (var j = 0; j < dependenciesCount; j++) { + x.push(ko.observable(-1)); + } + + var a = ko.pureComputed(function() { + var z = 0; + for (var j = 0; j < dependenciesCount; j++) { + z += x[j](); + } + return z; + }); + + var b = ko.pureComputed(function() { + return a(); + }); + + var c = ko.pureComputed(function() { + return a(); + }); + + var d = ko.observable(initialValue); + + var e = ko.pureComputed(function() { + return d() % 2 == 0 ? b() : c(); + }); + + return d; + } + + function setupitDepends() { + var x = []; + for (var j = 0; j < dependenciesCount; j++) { + x.push(itDepends.value(-1)); + } + + var a = itDepends.computed(function() { + var z = 0; + for (var j = 0; j < dependenciesCount; j++) { + z += x[j](); + } + return z; + }); + + var b = itDepends.computed(function() { + return a(); + }); + + var c = itDepends.computed(function() { + return a(); + }); + + var d = itDepends.value(-1); + + var e = itDepends.computed(function() { + return d() % 2 == 0 ? b() : c(); + }); + + var s = e.onChange(function(){ + + }); + + return d; + } + + var koobservable = setupKnockout(); + var idobservable = setupitDepends(); + }; + + var suite = new Benchmark.Suite('computed diamond updated ' + updatesCount + ' times with ' + dependenciesCount + ' hidden dependencies'); + + suite.add('knockout', function() { + for (var j = 0; j < updatesCount; j++) { + koobservable(j); + } + }); + + suite.add('itDepends', function() { + for (var j = 0; j < updatesCount; j++) { + idobservable.write(j); + } + }); + + return suite; +}; diff --git a/performance-tests/tests/computedDiamondUpdates/computedDiamond1000UpdatesWith500Dependencies.js b/performance-tests/tests/computedDiamondUpdates/computedDiamond1000UpdatesWith500Dependencies.js new file mode 100644 index 0000000..877cb7e --- /dev/null +++ b/performance-tests/tests/computedDiamondUpdates/computedDiamond1000UpdatesWith500Dependencies.js @@ -0,0 +1,3 @@ +var computedDiamondUpdate = require('../../scenarios/computedDiamondUpdate'); + +module.exports = computedDiamondUpdate(1000, 500); \ No newline at end of file