Skip to content

Commit

Permalink
Add immutability-helper benchmarks (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
danro authored and guigrpa committed Aug 22, 2018
1 parent 6a33a3c commit d6b6251
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 181 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"eslint-plugin-react": "^7.11.1",
"extract-docs": "^1.6.1",
"flow-bin": "^0.78.0",
"immutability-helper": "^2.7.1",
"immutable": "3.8.2",
"lodash": "^4.17.10",
"nyc": "^12.0.2",
Expand Down
52 changes: 51 additions & 1 deletion tools/benchmarks.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
process.env.NODE_ENV = 'production';
const _ = require('lodash');
const chalk = require('chalk');
// const Seamless = require('seamless-immutable')
// const Seamless = require('seamless-immutable')
const Seamless = require('seamless-immutable/seamless-immutable.production.min');
const Immutable = require('immutable');
const update = require('immutability-helper');
const timm = require('../lib/timm.min');

const INITIAL_OBJECT = {
Expand Down Expand Up @@ -131,6 +132,54 @@ _solImmutableSeamless = {
setAt: (arr, idx, val) => arr.set(idx, val),
};

_solImmutableUpdate = {
init: () => _.cloneDeep(INITIAL_OBJECT),
get: (obj, key) => obj[key],
set: (obj, key, val) => {
const config = {};
config[key] = {$set: val};
return update(obj, config);
},
getDeep: (obj, key1, key2) => obj[key1][key2],
setDeep: (obj, key1, key2, val) => {
const config = {};
config[key1] = {};
config[key1][key2] = {$set: val};
return update(obj, config);
},
getIn: _getIn,
setIn: (obj, path, val) => {
const config = {};
const len = path.length;
let child = config;
for (let n = 0; n < len; n++) {
child = child[path[n]] = n === len - 1 ? {$set: val} : {};
};
return update(obj, config);
},
merge: (obj1, obj2) => update(obj1, {$merge: obj2}),
mergeDeep: (obj1, obj2) => update(obj1, _nestedConfig(obj2)),
initArr: () => _.cloneDeep(INITIAL_ARRAY),
getAt: (arr, idx) => arr[idx],
setAt: (arr, idx, val) => {
const config = {};
config[idx] = {$set: val};
return update(arr, config);
},
};

const _nestedConfig = _.memoize(obj => {
return Object.keys(obj).reduce((result, key) => {
const val = obj[key];
if (typeof val === 'object' && val != null) {
result[key] = _nestedConfig(val);
} else {
result[key] = {$set: val};
}
return result;
}, {});
});

const _toggle = (solution, obj) =>
solution.set(obj, 'toggle', !solution.get(obj, 'toggle'));

Expand Down Expand Up @@ -323,3 +372,4 @@ _allTests('Mutable', _solMutable);
_allTests('Immutable (ImmutableJS)', _solImmutableJs);
_allTests('Immutable (timm)', _solImmutableTimm);
_allTests('Immutable (seamless-immutable)', _solImmutableSeamless);
_allTests('Immutable (immutability-helper)', _solImmutableUpdate);

0 comments on commit d6b6251

Please sign in to comment.