2017-04-19 00:47:53 +00:00
|
|
|
/* eslint no-console:0, no-unused-vars:0, import/no-extraneous-dependencies:0 */
|
|
|
|
/* global document */
|
2017-04-19 00:55:46 +00:00
|
|
|
const Benchmark = require('benchmark')
|
2015-08-10 06:25:19 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
const u = require('../lib')
|
|
|
|
const _ = require('lodash')
|
|
|
|
const { curry2, curry4 } = require('../lib/util/curry')
|
2015-08-10 06:25:19 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
const add4 = (a, b, c, d) => a + b + c + d
|
|
|
|
const add2 = (a, b) => a + b
|
|
|
|
const fakeCurryAdd = x => y => x + y
|
|
|
|
const lodashCurryAdd2 = _.curry(add2)
|
|
|
|
const updeepCurryAdd2 = curry2(add2)
|
|
|
|
const lodashCurryAdd4 = _.curry(add4)
|
|
|
|
const updeepCurryAdd4 = curry4(add4)
|
2015-08-12 06:55:15 +00:00
|
|
|
// const updeepCurryBig = curry.curryBig(add);
|
2015-08-10 06:25:19 +00:00
|
|
|
|
2017-04-19 00:55:46 +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') {
|
2017-04-19 00:55:46 +00:00
|
|
|
console.log(str)
|
|
|
|
const el = document.getElementById('perf')
|
|
|
|
el.innerHTML += str
|
2015-08-12 06:55:15 +00:00
|
|
|
}
|
2015-08-12 14:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createSuite(suiteName, tests) {
|
2017-04-19 00:55:46 +00:00
|
|
|
const suite = Benchmark.Suite() // eslint-disable
|
2015-08-12 14:22:45 +00:00
|
|
|
|
|
|
|
return () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
log(`<h2>${suiteName}</h2><ul>`)
|
2015-08-12 14:22:45 +00:00
|
|
|
|
|
|
|
_.each(tests, (fn, testName) => {
|
2017-04-19 00:55:46 +00:00
|
|
|
suite.add(testName, fn)
|
2015-08-12 14:22:45 +00:00
|
|
|
})
|
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
suite
|
|
|
|
.on('cycle', event => {
|
|
|
|
log(`<li>${String(event.target)}</li>`)
|
|
|
|
})
|
|
|
|
.on('complete', () => {
|
|
|
|
log('</ul>')
|
|
|
|
})
|
|
|
|
.run({ async: true })
|
|
|
|
}
|
|
|
|
}
|
2015-08-12 14:22:45 +00:00
|
|
|
|
|
|
|
const curryVsLodash = createSuite('Curry', {
|
2015-08-13 04:19:09 +00:00
|
|
|
'updeep curry': () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
updeepCurryAdd4(3)(4)(5)(6)
|
|
|
|
updeepCurryAdd4(3, 4, 5, 6)
|
|
|
|
updeepCurryAdd4(u._, 4, u._, 6)(3, 4)
|
|
|
|
updeepCurryAdd2(3)(4)
|
|
|
|
updeepCurryAdd2(3, 4)
|
2015-08-13 04:19:09 +00:00
|
|
|
},
|
|
|
|
'lodash curry': () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
lodashCurryAdd4(3)(4)(5)(6)
|
|
|
|
lodashCurryAdd4(3, 4, 5, 6)
|
|
|
|
lodashCurryAdd4(_, 4, _, 6)(3, 4)
|
|
|
|
lodashCurryAdd2(3)(4)
|
|
|
|
lodashCurryAdd2(3, 4)
|
2015-08-13 04:19:09 +00:00
|
|
|
},
|
2017-04-19 00:55:46 +00:00
|
|
|
})
|
2015-08-12 14:22:45 +00:00
|
|
|
|
|
|
|
const mapVsLodash = createSuite('Map', {
|
|
|
|
'_.map': () => _.map(array, fakeCurryAdd(8)),
|
|
|
|
'u.map': () => u.map(fakeCurryAdd(8), array),
|
2017-04-19 00:55:46 +00:00
|
|
|
})
|
2015-08-12 14:22:45 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
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)
|
2015-08-12 14:22:45 +00:00
|
|
|
const fnDestructure = (...args) => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const [a, b, c, d, e] = args
|
|
|
|
return fn(a, b, c, d, e)
|
|
|
|
}
|
2015-08-12 14:22:45 +00:00
|
|
|
const applyVsDestructure = createSuite('apply vs destructure', {
|
2016-01-13 16:19:43 +00:00
|
|
|
control: () => fnControl(1, 2, 3, 4, 5),
|
|
|
|
apply: () => fnApply(1, 2, 3, 4, 5),
|
|
|
|
destructure: () => fnDestructure(1, 2, 3, 4, 5),
|
2017-04-19 00:55:46 +00:00
|
|
|
})
|
2015-08-12 14:22:45 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
curryVsLodash()
|
|
|
|
mapVsLodash()
|
2015-08-12 15:35:10 +00:00
|
|
|
// applyVsDestructure();
|