Skip to content

Commit 5053399

Browse files
committed
chore: add bench w/ results
1 parent d02290f commit 5053399

File tree

5 files changed

+170
-1
lines changed

5 files changed

+170
-1
lines changed

bench/immutable.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const klona = require('klona');
2+
const assert = require('assert');
3+
const { Suite } = require('benchmark');
4+
const dset = require('../dist/dset');
5+
6+
const contenders = {
7+
'clean-set': require('clean-set'),
8+
'dset-klona': function (obj, key, val) {
9+
let copy = klona(obj);
10+
dset(copy, key, val);
11+
return copy;
12+
}
13+
};
14+
15+
console.log('Validation: ');
16+
Object.keys(contenders).forEach(name => {
17+
try {
18+
const input = {};
19+
const output = contenders[name](input, 'x.y.z', 'foobar');
20+
21+
assert.notEqual(output, input, 'new object');
22+
assert.deepStrictEqual(output, {
23+
x: {
24+
y: {
25+
z: 'foobar'
26+
}
27+
}
28+
}, 'expected output');
29+
30+
input.foo = 'bar';
31+
assert.notEqual(output.foo, 'bar', 'detached clone');
32+
33+
console.log(' ✔', name);
34+
} catch (err) {
35+
console.log(' ✘', name, `(FAILED @ "${err.message}")`);
36+
}
37+
});
38+
39+
40+
console.log('\nBenchmark:');
41+
const onCycle = e => console.log(' ' + e.target);
42+
const bench = new Suite({ onCycle });
43+
44+
Object.keys(contenders).forEach(name => {
45+
bench.add(name + ' '.repeat(12 - name.length), () => {
46+
contenders[name]({}, 'x.y.z', 'foobar');
47+
});
48+
});
49+
50+
bench.run();

bench/mutable.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const assert = require('assert');
2+
const { Suite } = require('benchmark');
3+
4+
const contenders = {
5+
'deep-set': require('deep-set'),
6+
'set-value': require('set-value'),
7+
'lodash/set': require('lodash/set'),
8+
'dset': require('../dist/dset'),
9+
};
10+
11+
console.log('Validation: ');
12+
Object.keys(contenders).forEach(name => {
13+
try {
14+
const input = {};
15+
contenders[name](input, 'x.y.z', 'foobar');
16+
assert.deepStrictEqual(input, {
17+
x: {
18+
y: {
19+
z: 'foobar'
20+
}
21+
}
22+
});
23+
24+
console.log(' ✔', name);
25+
} catch (err) {
26+
console.log(' ✘', name, `(FAILED)`);
27+
}
28+
});
29+
30+
31+
console.log('\nBenchmark:');
32+
const onCycle = e => console.log(' ' + e.target);
33+
const bench = new Suite({ onCycle });
34+
35+
Object.keys(contenders).forEach(name => {
36+
bench.add(name + ' '.repeat(12 - name.length), () => {
37+
contenders[name]({}, 'x.y.z', 'foobar');
38+
});
39+
});
40+
41+
bench.run();

bench/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"private": true,
3+
"devDependencies": {
4+
"benchmark": "2.1.4",
5+
"clean-set": "1.1.1",
6+
"deep-set": "1.0.1",
7+
"klona": "1.1.0",
8+
"lodash.set": "4.3.2",
9+
"set-value": "3.0.1"
10+
}
11+
}

bench/readme.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Benchmarks – Node.js
2+
3+
Below are the results while running this directory's suites on my machine with Node.js v10.13.0:
4+
5+
#### Mutable
6+
7+
> This is the default (and only) behavior
8+
9+
```
10+
Validation:
11+
✔ deep-set
12+
✔ set-value
13+
✔ lodash/set
14+
✔ dset
15+
16+
Benchmark:
17+
deep-set x 4,130,930 ops/sec ±2.30% (92 runs sampled)
18+
set-value x 4,905,191 ops/sec ±3.03% (91 runs sampled)
19+
lodash/set x 2,566,991 ops/sec ±0.99% (95 runs sampled)
20+
dset x 4,191,060 ops/sec ±0.24% (95 runs sampled)
21+
```
22+
23+
#### Immutable
24+
25+
> This combines `dset` with `klona`, as seen in the main README example
26+
27+
```
28+
Validation:
29+
✔ clean-set
30+
✔ dset-klona
31+
32+
Benchmark:
33+
clean-set x 5,771,347 ops/sec ±0.23% (96 runs sampled)
34+
dset-klona x 3,726,300 ops/sec ±1.53% (93 runs sampled)
35+
```

readme.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# dset [![Build Status](https://travis-ci.org/lukeed/dset.svg?branch=master)](https://travis-ci.org/lukeed/dset)
22

3-
> A tiny (161B) utility for safely writing deep Object values~!
3+
> A tiny (160B) utility for safely writing deep Object values~!
44
55
This module exposes two module definitions:
66

@@ -48,6 +48,25 @@ console.log(qux);
4848
//=> { a: [{ b: [1, 2] }] }
4949
```
5050

51+
## Mutability
52+
53+
As shown in the examples above, all `dset` interactions mutate the source object.
54+
55+
If you need immutable writes, please visit [`clean-set`](https://github.com/fwilkerson/clean-set) (182B).<br>
56+
Alternatively, you may pair `dset` with [`klona`](https://github.com/lukeed/klona), a 366B utility to clone your source(s). Here's an example pairing:
57+
58+
```js
59+
import klona from 'klona';
60+
import dset from 'dset';
61+
62+
export function deepset(obj, path, val) {
63+
let copy = klona(obj);
64+
dset(copy, path, val);
65+
return copy;
66+
}
67+
```
68+
69+
5170
## API
5271

5372
### dset(obj, path, val)
@@ -77,6 +96,19 @@ Type: `Any`
7796
The value that you want to set. Can be of any type!
7897

7998

99+
## Benchmarks
100+
101+
For benchmark results, check out the [`bench`](/bench) directory!
102+
103+
104+
## Related
105+
106+
- [dlv](https://github.com/developit/dlv) - safely read from deep properties in 120 bytes
107+
- [dequal](https://github.com/lukeed/dequal) - safely check for deep equality in 247 bytes
108+
- [klona](https://github.com/lukeed/klona) - quickly "deep clone" data in 200 to 330 bytes
109+
- [clean-set](https://github.com/fwilkerson/clean-set) - fast, immutable version of `dset` in 182 bytes
110+
111+
80112
## License
81113

82114
MIT © [Luke Edwards](https://lukeed.com)

0 commit comments

Comments
 (0)