Created
July 15, 2020 18:45
-
-
Save stephengfriend/bd384afaf0de20bf8eb65a3c592f73a1 to your computer and use it in GitHub Desktop.
Simple comparison of omit and pick patterns for performance curiosity
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 assert = require('assert'); | |
| function fnc() { | |
| assert.deepEqual(arguments?.[0], { 'c': 3 }) | |
| assert.deepEqual(arguments?.[1], { 'a': 1, 'b': '2' }) | |
| assert.ok(arguments?.[2] === 3, '!= 3') | |
| assert.ok(arguments?.[3] === 5, '!= 5') | |
| } | |
| const object = { 'a': 1, 'b': '2', 'c': 3 }; | |
| const start = process.hrtime(); | |
| for (let i = 0; i < 100000; i++) { | |
| fnc( | |
| Object.keys(object) | |
| .filter(k => !['a', 'b'].includes(k)) | |
| .reduce((result, k) => ({ [k]: object[k], ...result }), {}), | |
| { a: object.a, b: object.b }, | |
| 3, | |
| 5, | |
| ); | |
| } | |
| console.log(process.hrtime(start)); |
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 assert = require('assert'); | |
| const omit = require('lodash/omit'); | |
| const pick = require('lodash/pick'); | |
| function fnc() { | |
| assert.deepEqual(arguments?.[0], { 'c': 3 }) | |
| assert.deepEqual(arguments?.[1], { 'a': 1, 'b': '2' }) | |
| assert.ok(arguments?.[2] === 3, '!= 3') | |
| assert.ok(arguments?.[3] === 5, '!= 5') | |
| } | |
| const object = { 'a': 1, 'b': '2', 'c': 3 }; | |
| const start = process.hrtime(); | |
| for (let i = 0; i < 100000; i++) { | |
| fnc( | |
| omit(object, ['a', 'b']), | |
| pick(object, ['a', 'b']), | |
| 3, | |
| 5, | |
| ); | |
| } | |
| console.log(process.hrtime(start)); |
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 assert = require('assert'); | |
| const omit = require('lodash/omit'); | |
| const pick = require('lodash/pick'); | |
| function fnc() { | |
| assert.deepEqual(arguments?.[0], { 'c': 3 }) | |
| assert.deepEqual(arguments?.[1], { 'a': 1, 'b': '2' }) | |
| assert.ok(arguments?.[2] === 3, '!= 3') | |
| assert.ok(arguments?.[3] === 5, '!= 5') | |
| } | |
| const object = { 'a': 1, 'b': '2', 'c': 3 }; | |
| const start = process.hrtime(); | |
| for (let i = 0; i < 100000; i++) { | |
| fnc( | |
| omit(object, ['a', 'b']), | |
| omit(object, ['c']), | |
| 3, | |
| 5, | |
| ); | |
| } | |
| console.log(process.hrtime(start)); |
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 assert = require('assert'); | |
| function fnc() { | |
| assert.deepEqual(arguments?.[0], { 'c': 3 }) | |
| assert.deepEqual(arguments?.[1], { 'a': 1, 'b': '2' }) | |
| assert.ok(arguments?.[2] === 3, '!= 3') | |
| assert.ok(arguments?.[3] === 5, '!= 5') | |
| } | |
| const object = { 'a': 1, 'b': '2', 'c': 3 }; | |
| const start = process.hrtime(); | |
| for (let i = 0; i < 100000; i++) { | |
| const { a, b, ...rest } = object; | |
| fnc( | |
| rest, | |
| { a, b }, | |
| 3, | |
| 5, | |
| ); | |
| } | |
| console.log(process.hrtime(start)); |
Author
stephengfriend
commented
Jul 15, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment