Skip to content

Instantly share code, notes, and snippets.

@justinvdm
Last active January 6, 2018 22:28
Show Gist options
  • Save justinvdm/5391f2daa7745f220820a221eaddb5e6 to your computer and use it in GitHub Desktop.
Save justinvdm/5391f2daa7745f220820a221eaddb5e6 to your computer and use it in GitHub Desktop.
const assert = require('assert');
const partialLodash = require('lodash.partial');
var protoSlice = Array.prototype.slice;
function partialApply(fn, v) {
return function() {
var args = protoSlice.call(arguments);
args.unshift(v);
return fn.apply(null, args);
};
}
function partial2(fn, a) {
return function(b, c) {
return fn(a, b, c);
};
}
function add3(a, b, c) {
return a + b + c;
}
var fnApply = partialApply(add3, 21);
var fnLodash = partialLodash(add3, 21);
var fnPartial2 = partial2(add3, 21);
const test = () => {
assert.equal(26, fnApply(2, 3));
assert.equal(26, fnPartial2(2, 3));
assert.equal(26, fnLodash(2, 3));
};
suite('partial', () => {
const {N = 100000} = require('./conf');
before(() => {
test();
});
bench('apply', () => {
let i = -1;
while (++i < N) fnApply(i, i + 1);
});
bench('lodash.partial', () => {
let i = -1;
while (++i < N) fnLodash(i, i + 1);
});
bench('partial2', () => {
let i = -1;
while (++i < N) fnPartial2(i, i + 1);
});
});
partial
apply .......................................... 33 op/s
lodash.partial ................................. 287 op/s
partial2 ....................................... 11,773 op/s
Suites: 1
Benches: 3
Elapsed: 4,971.71 ms
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
};
{
"name": "bench-partial",
"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"
}
},
"lodash.partial": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/lodash.partial/-/lodash.partial-4.2.1.tgz",
"integrity": "sha1-SfPYz9qjv/izqR0SfpIyRUGJYdQ=",
"dev": true
},
"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
}
}
}
{
"name": "bench-partial",
"version": "1.0.0",
"description": "",
"main": "+partial.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": {
"lodash.partial": "^4.2.1",
"matcha": "^0.7.0"
},
"dependencies": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment