updeep-remeda/perf/index.js

72 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-08-12 14:22:45 +00:00
/* eslint no-console:0, no-unused-vars:0 */
2015-08-10 06:25:19 +00:00
const Benchmark = require('benchmark');
const u = require('../lib');
const _ = require('lodash');
2015-08-12 06:55:15 +00:00
const { curry2 } = require('../lib/util/curry');
2015-08-10 06:25:19 +00:00
const add = (x, y) => x + y;
const fakeCurryAdd = x => y => x + y;
const curryAdd = _.curry(add);
2015-08-12 06:55:15 +00:00
const updeepCurry = curry2(add);
// const updeepCurryBig = curry.curryBig(add);
2015-08-10 06:25:19 +00:00
const array = [0, 1, 2, 3, 4, 5];
2015-08-12 06:55:15 +00:00
// const doUpdate = u(x => x + 1);
2015-08-10 06:25:19 +00:00
2015-08-12 14:22:45 +00:00
function log(str) {
2015-08-12 06:55:15 +00:00
if (typeof document !== 'undefined') {
2015-08-12 14:22:45 +00:00
console.log(str);
2015-08-12 06:55:15 +00:00
const el = document.getElementById('perf');
2015-08-12 14:22:45 +00:00
el.innerHTML = el.innerHTML + str;
2015-08-12 06:55:15 +00:00
}
2015-08-12 14:22:45 +00:00
}
function createSuite(suiteName, tests) {
const suite = Benchmark.Suite(); // eslint-disable
return () => {
log(`<h2>${suiteName}</h2><ul>`);
_.each(tests, (fn, testName) => {
suite.add(testName, fn);
});
suite.on('cycle', (event) => {
log(`<li>${String(event.target)}</li>`);
})
.on('complete', () => {
log('</ul>');
})
.run({ async: true });
};
}
const curryVsLodash = createSuite('Curry', {
'updeep curry partial call': () => updeepCurry(3)(4),
'lodash curry partial call': () => curryAdd(3)(4),
});
const mapVsLodash = createSuite('Map', {
'_.map': () => _.map(array, fakeCurryAdd(8)),
'u.map': () => u.map(fakeCurryAdd(8), array),
});
const fn = (a, b, c, d, e) => a + b + c + d + e;
const fnControl = (a, b, c, d, e) => fn(a, b, c, d, e);
const fnApply = (...args) => fn(...args);
const fnDestructure = (...args) => {
const [a, b, c, d, e] = args;
return fn(a, b, c, d, e);
};
const applyVsDestructure = createSuite('apply vs destructure', {
'control': () => fnControl(1, 2, 3, 4, 5),
'apply': () => fnApply(1, 2, 3, 4, 5),
'destructure': () => fnDestructure(1, 2, 3, 4, 5),
});
// curryVsLodash();
// mapVsLodash();
applyVsDestructure();