Last active
August 13, 2018 18:39
-
-
Save danactive/89e8b5bef2e13f4c9472edbec67ce7c0 to your computer and use it in GitHub Desktop.
Compare various merge modules (no timed benchmarks just functional differences)
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
| // npm i deepmerge object-merge merge underscore --no-save | |
| const mergers = { | |
| deepmerge: require('deepmerge'), | |
| 'deepmerge.all': (...args) => require('deepmerge').all(args), | |
| 'object-merge': require('object-merge'), | |
| 'Object.assign': Object.assign, | |
| merge: require('merge'), | |
| 'merge (recursive)': require('merge').recursive, | |
| spread: (...args) => ({ ...args[0], ...args[1], ...args[2] }), | |
| 'underscore (extend)': require('underscore').extend, | |
| 'underscore (extendOwn)': require('underscore').extendOwn, | |
| }; | |
| function test(merge) { | |
| const first = { | |
| firstNull: null, | |
| destNull: 'not null', | |
| firstUndefined: undefined, | |
| destUndefined: 'not undefined', | |
| boolean: true, | |
| array: [1, 2, 3, null, undefined], | |
| thrice: 'first', | |
| nestedObj: { | |
| bananas: 1, | |
| oranges: 1, | |
| referenceTest: 'first', | |
| }, | |
| }; | |
| const second = { | |
| firstNull: 'not null', | |
| destNull: null, | |
| firstUndefined: 'not undefined', | |
| destUndefined: undefined, | |
| boolean: false, | |
| array: [5, 6, 7], | |
| thrice: 'second', | |
| nestedObj: { | |
| apples: 1, | |
| oranges: 2, | |
| referenceTest: 'second', | |
| }, | |
| }; | |
| const third = { | |
| thrice: 'third', | |
| fromThird: true, | |
| }; | |
| const merged = merge(first, second, third); | |
| first.nestedObj.referenceTest = 'changed first'; | |
| second.nestedObj.referenceTest = 'changed second'; | |
| console.log(merged); | |
| } | |
| console.log('Results'); | |
| Object.keys(mergers).forEach((name) => { | |
| console.log(); | |
| console.log(`Testing: ${name}`); | |
| test(mergers[name]); | |
| }); |
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
| Results | |
| Testing: deepmerge | |
| { firstNull: 'not null', | |
| destNull: null, | |
| firstUndefined: 'not undefined', | |
| destUndefined: undefined, | |
| boolean: false, | |
| array: [ 1, 2, 3, null, undefined, 5, 6, 7 ], | |
| thrice: 'second', | |
| nestedObj: | |
| { bananas: 1, oranges: 2, referenceTest: 'second', apples: 1 } } | |
| Testing: deepmerge.all | |
| { firstNull: 'not null', | |
| destNull: null, | |
| firstUndefined: 'not undefined', | |
| destUndefined: undefined, | |
| boolean: false, | |
| array: [ 1, 2, 3, null, undefined, 5, 6, 7 ], | |
| thrice: 'third', | |
| nestedObj: | |
| { bananas: 1, oranges: 2, referenceTest: 'second', apples: 1 }, | |
| fromThird: true } | |
| Testing: object-merge | |
| { firstNull: 'not null', | |
| destNull: null, | |
| firstUndefined: 'not undefined', | |
| destUndefined: undefined, | |
| boolean: false, | |
| array: [ 5, 6, 7, null, undefined ], | |
| thrice: 'third', | |
| nestedObj: | |
| { bananas: 1, oranges: 2, referenceTest: 'second', apples: 1 }, | |
| fromThird: true } | |
| Testing: Object.assign | |
| { firstNull: 'not null', | |
| destNull: null, | |
| firstUndefined: 'not undefined', | |
| destUndefined: undefined, | |
| boolean: false, | |
| array: [ 5, 6, 7 ], | |
| thrice: 'third', | |
| nestedObj: { apples: 1, oranges: 2, referenceTest: 'changed second' }, | |
| fromThird: true } | |
| Testing: merge | |
| { firstNull: 'not null', | |
| destNull: null, | |
| firstUndefined: 'not undefined', | |
| destUndefined: undefined, | |
| boolean: false, | |
| array: [ 5, 6, 7 ], | |
| thrice: 'third', | |
| nestedObj: { apples: 1, oranges: 2, referenceTest: 'changed second' }, | |
| fromThird: true } | |
| Testing: merge (recursive) | |
| { firstNull: 'not null', | |
| destNull: null, | |
| firstUndefined: 'not undefined', | |
| destUndefined: undefined, | |
| boolean: false, | |
| array: [ 5, 6, 7 ], | |
| thrice: 'third', | |
| nestedObj: | |
| { bananas: 1, | |
| oranges: 2, | |
| referenceTest: 'changed first', | |
| apples: 1 }, | |
| fromThird: true } | |
| Testing: spread | |
| { firstNull: 'not null', | |
| destNull: null, | |
| firstUndefined: 'not undefined', | |
| destUndefined: undefined, | |
| boolean: false, | |
| array: [ 5, 6, 7 ], | |
| thrice: 'third', | |
| nestedObj: { apples: 1, oranges: 2, referenceTest: 'changed second' }, | |
| fromThird: true } | |
| Testing: underscore (extend) | |
| { firstNull: 'not null', | |
| destNull: null, | |
| firstUndefined: 'not undefined', | |
| destUndefined: undefined, | |
| boolean: false, | |
| array: [ 5, 6, 7 ], | |
| thrice: 'third', | |
| nestedObj: { apples: 1, oranges: 2, referenceTest: 'changed second' }, | |
| fromThird: true } | |
| Testing: underscore (extendOwn) | |
| { firstNull: 'not null', | |
| destNull: null, | |
| firstUndefined: 'not undefined', | |
| destUndefined: undefined, | |
| boolean: false, | |
| array: [ 5, 6, 7 ], | |
| thrice: 'third', | |
| nestedObj: { apples: 1, oranges: 2, referenceTest: 'changed second' }, | |
| fromThird: true } |
Author
Author
Updated with JavaScript native spread
Author
Updated with test using underscore. The results are disappointing as bananas is missing from spread and underscore
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
deepmerge.all()andobject-mergetreat arrays with different behaviour but otherwise with identical structure