Last active
January 7, 2018 10:53
-
-
Save justinvdm/cfd8ece2412952fc9d268d95a33197b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require('assert'); | |
const flow = fns => v0 => { | |
const n = fns.length; | |
let i = -1; | |
let v = v0; | |
while (++i < n) v = fns[i](v); | |
return v; | |
}; | |
const pipe = (fn1, fn2) => v => fn2(fn1(v)); | |
const pipeAcc = (fn1, fn2) => { | |
const fns = (fn1._fns || [fn1]).concat(fn2._fns || [fn2]); | |
const fn = flow(fns); | |
fn._fns = fns; | |
return fn; | |
}; | |
const fnList = [ | |
v => v + 1, v => v + 2, v => v + 3, v => v + 4, v => v + 5, v => v + 6, | |
v => v + 7, v => v + 8, v => v + 9, v => v + 10, v => v + 11, v => v + 13, | |
v => v + 14, v => v + 15, v => v + 16, v => v + 17, v => v + 18, v => v + 19, | |
v => v + 20, v => v + 21, v => v + 22, v => v + 23 | |
]; | |
const fnFlow = flow(fnList); | |
const fnPipe = fnList.reduce((fn2, fn1) => pipe(fn1, fn2)); | |
const fnPipeAcc = fnList.reduce((fn2, fn1) => pipeAcc(fn1, fn2)); | |
const test = () => { | |
assert.equal(264, fnFlow(0)); | |
assert.equal(264, fnPipe(0)); | |
assert.equal(264, fnPipeAcc(0)); | |
}; | |
suite('function-composition', () => { | |
const {N = 100000} = require('./conf'); | |
before(() => { | |
test(); | |
}); | |
bench('flow', () => { | |
let i = -1; | |
while (++i < N) fnFlow(i); | |
}); | |
bench('pipe', () => { | |
let i = -1; | |
while (++i < N) fnPipe(i); | |
}); | |
bench('pipeAccum', () => { | |
let i = -1; | |
while (++i < N) fnPipeAcc(i); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function-composition | |
flow ........................................... 41 op/s | |
pipe ........................................... 27 op/s | |
pipeAccum ...................................... 41 op/s | |
Suites: 1 | |
Benches: 3 | |
Elapsed: 8,951.62 ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let N; | |
if (process.env.BENCH_N) N = process.env.BENCH_N; | |
if (process.env.TEST) { | |
set('type', 'static'); | |
set('iterations', 1); | |
N = 1; | |
} | |
module.exports = { | |
N | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "bench-function-composition", | |
"version": "1.0.0", | |
"lockfileVersion": 1, | |
"requires": true, | |
"dependencies": { | |
"drip": { | |
"version": "1.1.0", | |
"resolved": "https://registry.npmjs.org/drip/-/drip-1.1.0.tgz", | |
"integrity": "sha1-zO+x5obYb8EVtwyewSb4+HG9/X4=", | |
"dev": true, | |
"requires": { | |
"tea-concat": "0.1.0" | |
} | |
}, | |
"electron": { | |
"version": "0.4.1", | |
"resolved": "https://registry.npmjs.org/electron/-/electron-0.4.1.tgz", | |
"integrity": "sha1-p4oFGniC9OVC1uIH2KGnMHazAUQ=", | |
"dev": true, | |
"requires": { | |
"drip": "1.1.0" | |
} | |
}, | |
"matcha": { | |
"version": "0.7.0", | |
"resolved": "https://registry.npmjs.org/matcha/-/matcha-0.7.0.tgz", | |
"integrity": "sha1-E/gFQJs3vlcDLIRYZDvxUjumjdo=", | |
"dev": true, | |
"requires": { | |
"electron": "0.4.1", | |
"v8-argv": "0.1.0" | |
} | |
}, | |
"tea-concat": { | |
"version": "0.1.0", | |
"resolved": "https://registry.npmjs.org/tea-concat/-/tea-concat-0.1.0.tgz", | |
"integrity": "sha1-6i6QdAD914pjNM4CD6PGlQLZnoQ=", | |
"dev": true | |
}, | |
"v8-argv": { | |
"version": "0.1.0", | |
"resolved": "https://registry.npmjs.org/v8-argv/-/v8-argv-0.1.0.tgz", | |
"integrity": "sha1-rfd3pS29w9qciclGXlntXzy22ak=", | |
"dev": true | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "bench-function-composition", | |
"version": "1.0.0", | |
"description": "", | |
"main": "++bench-function-composition.js", | |
"scripts": { | |
"test": "TEST=1 npm run bench", | |
"start": "npm run bench --silent | tee +results.txt", | |
"bench": "matcha -R plain *.js" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"matcha": "^0.7.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment