Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const { add, complete, cycle, save, suite } = require('benny')
const path = require('path')
const delay = (seconds) =>
new Promise((resolve) => setTimeout(resolve, seconds * 1000))
module.exports = suite(
'Async madness',
add('Async benchmark without setup', async () => {
// You can use await or return - works the same,
// (async function always returns a Promise)
await delay(0.5) // Resulting in 2 ops/s
}),
add('Async benchmark without setup - many async operations', async () => {
await delay(0.5)
await delay(0.5)
// Resulting in 1 ops/s
}),
add('Async benchmark with some setup', async () => {
await delay(2) // Setup can be async, it will not affect the results
return async () => {
await delay(0.5) // Still 2 ops/s
_.reduce((a, b) => a + b, 0)(input)
}),
add('ramda', () => {
R.reduce((a, b) => a + b, 0)(input)
}),
add('@arrows/array', () => {
A.reduce((a, b) => a + b, 0)(input)
}),
add('@arrows/array first', () => {
A.reduce.first((a, b) => a + b)(input)
}),
add('rambda', () => {
RB.reduce((a, b) => a + b, 0)(input)
}),
cycle(),
complete(),
save({ file: 'reduce' }),
save({ file: 'reduce', format: 'chart.html' }),
)
return () => fn({ foo: 3, bar: { baz: 3, bat: [3, 3, 3] } })
},
),
add(
'Execute multimethod with ten complex caseVal methods - last matching',
() => {
const methods = createNMethodsWithComplexCaseVal(10)
const fn = multi(...methods)
return () => fn({ foo: 10, bar: { baz: 10, bat: [10, 10, 10] } })
},
),
add(
'Execute multimethod with a hundred complex caseVal methods - last matching',
() => {
const methods = createNMethodsWithComplexCaseVal(100)
const fn = multi(...methods)
return () => fn({ foo: 100, bar: { baz: 100, bat: [100, 100, 100] } })
},
),
add(
'Execute multimethod with a thousand complex caseVal methods - last matching',
() => {
const methods = createNMethodsWithComplexCaseVal(1000)
const fn = multi(...methods)
return () =>
}, data)
}
}),
add('Immutable.js', () => {
const data = Immutable.fromJS(initializeTestData())
return () => {
const newData = data
.setIn(['foo', 'bar', 'baz'], 'yo')
.setIn(['foo', 'bar', 'bat', 1], 7)
.toJS()
}
}),
add('Clone - RFDC', () => {
const data = initializeTestData()
return () => {
const newData = cloneRFDC(data)
newData.foo.bar.baz = 'yo'
newData.foo.bar.bat[1] = 7
}
}),
add('Clone - clone', () => {
const data = initializeTestData()
return () => {
const newData = clone(data)
newData.foo.bar.baz = 'yo'
newData.foo.bar.bat[1] = 7
}
}
}),
add('Immer', () => {
const data = initializeTestData()
return () => {
const newData = produce(data, (draft) => {
draft.foo.bar.baz = 'yo'
draft.foo.bar.bat[1] = 7
})
}
}),
add('Transmutable', () => {
const data = initializeTestData()
return () => {
const newData = transform((draft) => {
draft.foo.bar.baz = 'yo'
draft.foo.bar.bat[1] = 7
}, data)
}
}),
add('Immutable.js', () => {
const data = Immutable.fromJS(initializeTestData())
return () => {
const newData = data
.setIn(['foo', 'bar', 'baz'], 'yo')
const RB = require('rambda')
const input = Array.from({ length: 100 }, (_, i) => i)
module.exports = suite(
'Reduce implementations comparison',
add('lodash/fp', () => {
_.reduce((a, b) => a + b, 0)(input)
}),
add('ramda', () => {
R.reduce((a, b) => a + b, 0)(input)
}),
add('@arrows/array', () => {
A.reduce((a, b) => a + b, 0)(input)
}),
add('@arrows/array first', () => {
A.reduce.first((a, b) => a + b)(input)
}),
add('rambda', () => {
RB.reduce((a, b) => a + b, 0)(input)
}),
cycle(),
complete(),
save({ file: 'reduce' }),
save({ file: 'reduce', format: 'chart.html' }),
)
export default suite(
'Multimethod execution',
add.skip('Execute identity function for reference', () => {
const fn = (x) => x
return () => fn('foo')
}),
add('Execute multimethod with default method as value only', () => {
const fn = multi(method('default'))
return () => fn('foo')
}),
add(
'Execute multimethod with one simple val/val method - last matching',
() => {
const methods = createNMethodsWithSimpleValVal(1)
const fn = multi(...methods)
return () => fn(1)
},
),
add(
'Execute multimethod with three simple val/val methods - last matching',
() => {
const methods = createNMethodsWithSimpleValVal(3)
const fn = multi(...methods)
return () => fn(3)
})
}
}),
add('Transmutable', () => {
const data = initializeTestData()
return () => {
const newData = transform((draft) => {
draft.foo.bar.baz = 'yo'
draft.foo.bar.bat[1] = 7
}, data)
}
}),
add('Immutable.js', () => {
const data = Immutable.fromJS(initializeTestData())
return () => {
const newData = data
.setIn(['foo', 'bar', 'baz'], 'yo')
.setIn(['foo', 'bar', 'bat', 1], 7)
.toJS()
}
}),
add('Clone - RFDC', () => {
const data = initializeTestData()
return () => {
const newData = cloneRFDC(data)
newData.foo.bar.baz = 'yo'
const fn = multi(method('default'))
return () => fn('foo')
}),
add(
'Execute multimethod with one simple val/val method - last matching',
() => {
const methods = createNMethodsWithSimpleValVal(1)
const fn = multi(...methods)
return () => fn(1)
},
),
add(
'Execute multimethod with three simple val/val methods - last matching',
() => {
const methods = createNMethodsWithSimpleValVal(3)
const fn = multi(...methods)
return () => fn(3)
},
),
add(
'Execute multimethod with ten simple val/val methods - last matching',
() => {
const methods = createNMethodsWithSimpleValVal(10)
const fn = multi(...methods)
return () => fn(10)
save({ file: 'all-equal-hundred', folder: path.join(__dirname, 'results') }),
save({
file: 'all-equal-hundred',
folder: path.join(__dirname, 'results'),
format: 'chart.html',
}),
)
suite(
'All equal - million elements',
add('Imperative', () => {
allEqualImperative(inputMillion)
}),
add('Declarative', () => {
allEqualEveryDeclarative(inputMillion)
}),
cycle(),
complete(),
save({ file: 'all-equal-million', folder: path.join(__dirname, 'results') }),
save({
file: 'all-equal-million',
folder: path.join(__dirname, 'results'),
format: 'chart.html',
}),
)