Clean up benchmarks

main
Aaron Jensen 2015-08-12 07:22:45 -07:00
parent 3857d33d1b
commit f32dba4a03
2 changed files with 54 additions and 51 deletions

View File

@ -9,6 +9,6 @@
<div>Running tests...</div>
<div id='perf'></div>
<script src='/assets/perf.js'></script>
<div>Done!</div>
<p>Done!</p>
</body>
</html>

View File

@ -1,11 +1,10 @@
/* eslint no-console:0, no-unused-vars:0 */
const Benchmark = require('benchmark');
const u = require('../lib');
const _ = require('lodash');
const { curry2 } = require('../lib/util/curry');
const suite = Benchmark.Suite(); // eslint-disable
const add = (x, y) => x + y;
const fakeCurryAdd = x => y => x + y;
const curryAdd = _.curry(add);
@ -15,54 +14,58 @@ const updeepCurry = curry2(add);
const array = [0, 1, 2, 3, 4, 5];
// const doUpdate = u(x => x + 1);
suite
.add('updeep curry partial call', () => {
updeepCurry(3)(4);
})
.add('lodash curry partial call', () => {
curryAdd(3)(4);
})
.add('_.map', () => {
_.map(array, fakeCurryAdd(8));
})
.add('u.map', () => {
u.map(fakeCurryAdd(8), array);
})
// .add('updeep curry', () => {
// u.map(curryAdd(3), array);
// })
// .add('updeep big curry', () => {
// u.map(curryAdd(3), array);
// })
// .add('no curry', () => {
// u.map(x => x + 1, array);
// })
// .add('regular function call', () => {
// add(3, 4);
// })
// .add('updeep full curry', () => {
// updeepCurry(3, 4);
// })
// .add('updeep big full curry', () => {
// updeepCurryBig(3, 4);
// })
// .add('curry full call', () => {
// curryAdd(3, 4);
// })
// .add('fake curry', () => {
// fakeCurryAdd(3)(4);
// })
// .add('_.map no changes', () => {
// _.map(array, x => x);
// })
// .add('u.map no changes', () => {
// u.map(x => x, array);
// })
.on('cycle', (event) => {
function log(str) {
if (typeof document !== 'undefined') {
console.log(str);
const el = document.getElementById('perf');
el.innerHTML = el.innerHTML + String(event.target) + '<br>';
el.innerHTML = el.innerHTML + str;
}
})
.run({ async: true });
}
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();