Skip to content

Instantly share code, notes, and snippets.

@justinvdm
Last active January 6, 2018 22:29
Show Gist options
  • Save justinvdm/8642122a4e334f0e3ef7919da65e1c70 to your computer and use it in GitHub Desktop.
Save justinvdm/8642122a4e334f0e3ef7919da65e1c70 to your computer and use it in GitHub Desktop.
const assert = require('assert');
function Foo(val) {
this.val = val;
}
const acceptInstanceOf = (cls, acceptFn, rejectFn) => {
return v => v instanceof cls
? acceptFn(v)
: rejectFn(v);
}
const acceptType = (type, acceptFn, rejectFn) => {
return v => v.type === type
? acceptFn(v)
: rejectFn(v);
}
const acceptMaybeType = (type, acceptFn, rejectFn) => {
return v => v && v.type === type
? acceptFn(v)
: rejectFn(v);
};
const fnInstance = acceptInstanceOf(Foo, v => v.val * 2, v => v * 3);
const fnType = acceptType('foo', v => v.val * 2, v => v.val * 3);
const fnMaybeType = acceptMaybeType('foo', v => v.val * 2, v => v * 3);
const test = () => {
assert.equal(4, fnInstance(new Foo(2)), 4);
assert.equal(6, fnInstance(2));
assert.equal(4, fnType({
type: 'foo',
val: 2
}));
assert.equal(6, fnType({
type: 'bar',
val: 2
}));
assert.equal(4, fnMaybeType({
type: 'foo',
val: 2
}));
assert.equal(6, fnMaybeType(2));
};
suite('instanceof-vs-obj-types', () => {
const {N = 100000} = require('./conf');
before(() => {
test();
});
bench('instanceof', () => {
let i = -1;
while (++i < N)
if (i % N) fnInstance(new Foo(i));
else fnInstance(i);
});
bench('type', () => {
let i = -1;
while (++i < N)
if (i % N)
fnType({
type: 'foo',
val: i
});
else
fnType({
type: 'bar',
val: i
});
});
bench('maybeType', () => {
let i = -1;
while (++i < N)
if (i % N)
fnType({
type: 'foo',
val: i
});
else
fnType(i);
});
});
instanceof-vs-obj-types
instanceof ..................................... 1,000 op/s
type ........................................... 3,168 op/s
maybeType ...................................... 3,403 op/s
Suites: 1
Benches: 3
Elapsed: 2,641.36 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-instanceof-vs-obj-types",
"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
}
}
}
{
"name": "bench-instanceof-vs-obj-types",
"version": "1.0.0",
"description": "",
"main": "+instanceof-vs-obj-types.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