Created
December 6, 2019 15:39
-
-
Save Aschen/8aa9335920e7b7cfb3f60ad8c8babb05 to your computer and use it in GitHub Desktop.
Benchmarking merging object
This file contains hidden or 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 Benchmark = require('benchmark') | |
| const _ = require('lodash'); | |
| const suite = new Benchmark.Suite | |
| const obj1 = { | |
| nested: { | |
| object: { | |
| value: 'string' | |
| } | |
| }, | |
| value: 42 | |
| }; | |
| const obj2 = { | |
| something: 'foobar', | |
| value: 21, | |
| detsen: { | |
| tcejbo: { | |
| eulav: 'gnirts' | |
| } | |
| } | |
| }; | |
| suite | |
| .add('Object.assign', () => { | |
| const res = Object.assign({}, obj1, obj2); | |
| }) | |
| .add('Destructuring', () => { | |
| const res = { ...obj1, ...obj2 }; | |
| }) | |
| .add('lodash.merge', () => { | |
| const res = _.merge({}, obj1, obj2); | |
| }) | |
| .on('cycle', function(event) { | |
| console.log(String(event.target)); | |
| }) | |
| .on('complete', function() { | |
| console.log('Fastest is ' + this.filter('fastest').map('name')); | |
| }) | |
| .run(); |
This file contains hidden or 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
| $ node --version | |
| v12.13.0 | |
| $ node nodejs/assign-vs-destructuring.js | |
| Object.assign x 9,002,988 ops/sec ±4.35% (84 runs sampled) | |
| Destructuring x 13,389,910 ops/sec ±0.96% (92 runs sampled) | |
| lodash.merge x 240,255 ops/sec ±1.12% (91 runs sampled) | |
| Fastest is Destructuring |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment