Skip to content

Commit

Permalink
Update benchmark (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thor Sedeke authored and sindresorhus committed Jun 20, 2018
1 parent 9883dfc commit fe1feb2
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
176 changes: 90 additions & 86 deletions bench.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict';
/* globals bench */

const Benchmark = require('benchmark');
const m = require('.');

bench('get', () => {
const suite = new Benchmark.Suite();

suite.add('get', () => {
const f1 = {foo: {bar: 1}};
m.get(f1);
f1[''] = 'foo';
Expand Down Expand Up @@ -48,112 +51,113 @@ bench('get', () => {
m.get('foo', 'foo.bar', false);
m.get([], 'foo.bar', false);
m.get(undefined, 'foo.bar', false);
});

bench('set', () => {
const func = () => 'test';
let f1 = {};
})
.add('set', () => {
const func = () => 'test';
let f1 = {};

m.set(f1, 'foo', 2);
m.set(f1, 'foo', 2);

f1 = {foo: {bar: 1}};
m.set(f1, 'foo.bar', 2);
f1 = {foo: {bar: 1}};
m.set(f1, 'foo.bar', 2);

m.set(f1, 'foo.bar.baz', 3);
m.set(f1, 'foo.bar.baz', 3);

m.set(f1, 'foo.bar', 'test');
m.set(f1, 'foo.bar', 'test');

m.set(f1, 'foo.bar', null);
m.set(f1, 'foo.bar', null);

m.set(f1, 'foo.bar', false);
m.set(f1, 'foo.bar', false);

m.set(f1, 'foo.bar', undefined);
m.set(f1, 'foo.bar', undefined);

m.set(f1, 'foo.fake.fake2', 'fake');
m.set(f1, 'foo.fake.fake2', 'fake');

m.set(f1, 'foo.function', func);
m.set(f1, 'foo.function', func);

function fn() {}
m.set(fn, 'foo.bar', 1);

f1.fn = fn;
m.set(f1, 'fn.bar.baz', 2);
function fn() {}
m.set(fn, 'foo.bar', 1);

const f2 = {foo: null};
m.set(f2, 'foo.bar', 2);
f1.fn = fn;
m.set(f1, 'fn.bar.baz', 2);

const f3 = {};
m.set(f3, '', 3);
const f2 = {foo: null};
m.set(f2, 'foo.bar', 2);

m.set(f1, 'foo\\.bar.baz', true);
const f3 = {};
m.set(f3, '', 3);

m.set(f1, 'fo\\.ob\\.ar.baz', true);
});
m.set(f1, 'foo\\.bar.baz', true);

bench('delete', () => {
const func = () => 'test';
func.foo = 'bar';
m.set(f1, 'fo\\.ob\\.ar.baz', true);
})
.add('delete', () => {
const func = () => 'test';
func.foo = 'bar';

const inner = {
a: 'a',
b: 'b',
c: 'c',
func
};
const inner = {
a: 'a',
b: 'b',
c: 'c',
func
};

const f1 = {
foo: {
bar: {
baz: inner
const f1 = {
foo: {
bar: {
baz: inner
}
},
top: {
dog: 'sindre'
}
},
top: {
dog: 'sindre'
}
};
};

m.delete(f1, 'foo.bar.baz.c');
m.delete(f1, 'foo.bar.baz.c');

m.delete(f1, 'top');
m.delete(f1, 'top');

m.delete(f1, 'foo.bar.baz.func.foo');
m.delete(f1, 'foo.bar.baz.func.foo');

m.delete(f1, 'foo.bar.baz.func');
m.delete(f1, 'foo.bar.baz.func');

m.set(f1, 'foo\\.bar.baz', true);
m.delete(f1, 'foo\\.bar.baz');
m.set(f1, 'foo\\.bar.baz', true);
m.delete(f1, 'foo\\.bar.baz');

const f2 = {};
m.set(f2, 'foo.bar\\.baz', true);
m.delete(f2, 'foo.bar\\.baz');

f2.dotted = {
sub: {
'dotted.prop': 'foo',
other: 'prop'
}
};
m.delete(f2, 'dotted.sub.dotted\\.prop');
});

bench('has', () => {
const f1 = {foo: {bar: 1}};
m.has(f1);
m.has(f1, 'foo');
m.has({foo: 1}, 'foo');
m.has({foo: null}, 'foo');
m.has({foo: undefined}, 'foo');
m.has({foo: {bar: true}}, 'foo.bar');
m.has({foo: {bar: {baz: true}}}, 'foo.bar.baz');
m.has({foo: {bar: {baz: null}}}, 'foo.bar.baz');
m.has({foo: {bar: 'a'}}, 'foo.fake.fake2');
const f2 = {};
m.set(f2, 'foo.bar\\.baz', true);
m.delete(f2, 'foo.bar\\.baz');

function fn() {}
fn.foo = {bar: 1};
m.has(fn);
m.has(fn, 'foo');
m.has(fn, 'foo.bar');

m.has({'foo.baz': {bar: true}}, 'foo\\.baz.bar');
m.has({'fo.ob.az': {bar: true}}, 'fo\\.ob\\.az.bar');
});
f2.dotted = {
sub: {
'dotted.prop': 'foo',
other: 'prop'
}
};
m.delete(f2, 'dotted.sub.dotted\\.prop');
})
.add('has', () => {
const f1 = {foo: {bar: 1}};
m.has(f1);
m.has(f1, 'foo');
m.has({foo: 1}, 'foo');
m.has({foo: null}, 'foo');
m.has({foo: undefined}, 'foo');
m.has({foo: {bar: true}}, 'foo.bar');
m.has({foo: {bar: {baz: true}}}, 'foo.bar.baz');
m.has({foo: {bar: {baz: null}}}, 'foo.bar.baz');
m.has({foo: {bar: 'a'}}, 'foo.fake.fake2');

function fn() {}
fn.foo = {bar: 1};
m.has(fn);
m.has(fn, 'foo');
m.has(fn, 'foo.bar');

m.has({'foo.baz': {bar: true}}, 'foo\\.baz.bar');
m.has({'fo.ob.az': {bar: true}}, 'fo\\.ob\\.az.bar');
}).on('cycle', event => {
console.log(String(event.target));
}).on('complete', () => {
console.log('Finished');
}).run({async: true});
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"scripts": {
"test": "xo && ava",
"bench": "matcha bench.js"
"bench": "node bench.js"
},
"files": [
"index.js"
Expand All @@ -39,7 +39,8 @@
},
"devDependencies": {
"ava": "*",
"matcha": "^0.7.0",
"benchmark": "^2.1.4",
"microtime": "^2.1.8",
"xo": "*"
}
}

0 comments on commit fe1feb2

Please sign in to comment.