Created
May 26, 2016 02:45
-
-
Save nightink/359007fb25c63a7c884f31b94dc5891e to your computer and use it in GitHub Desktop.
omit vs pick
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
| 'use strict'; | |
| const Benchmark = require('benchmark'); | |
| const benchmarks = require('beautify-benchmark'); | |
| const suite = new Benchmark.Suite; | |
| const Copy = require('copy-to'); | |
| /** | |
| * omit keys in src | |
| * | |
| * @api: public | |
| */ | |
| var slice = Array.prototype.slice; | |
| Copy.prototype.omit = function(keys) { | |
| if (!Array.isArray(keys)) { | |
| keys = slice.call(arguments); | |
| } | |
| if (keys.length) { | |
| var originalKeys = Object.keys(this.src); | |
| this.keys = originalKeys.filter(function(key) { | |
| return keys.indexOf(key) === -1; | |
| }); | |
| } | |
| return this; | |
| }; | |
| suite | |
| .add('new Object', function () { | |
| const obj = { | |
| name: 'copy-to', | |
| name1: 'copy-to', | |
| name2: 'copy-to', | |
| name3: 'copy-to', | |
| name4: 'copy-to', | |
| }; | |
| ({ | |
| name: obj.name, | |
| name1: obj.name1, | |
| name2: obj.name2, | |
| }) | |
| }) | |
| .add('pick', function() { | |
| const obj = { | |
| name: 'copy-to', | |
| name1: 'copy-to', | |
| name2: 'copy-to', | |
| name3: 'copy-to', | |
| name4: 'copy-to', | |
| }; | |
| Copy(obj).pick('name', 'name1', 'name3').to(); | |
| }) | |
| .add('omit v1', function () { | |
| const obj = { | |
| name: 'copy-to', | |
| name1: 'copy-to', | |
| name2: 'copy-to', | |
| name3: 'copy-to', | |
| name4: 'copy-to', | |
| }; | |
| Copy(obj).omit('name4').to(); | |
| }) | |
| // add listeners | |
| .on('cycle', function(event) { | |
| benchmarks.add(event.target); | |
| }) | |
| .on('complete', function() { | |
| benchmarks.log('Fastest is ' + this.filter('fastest').map('name')); | |
| }) | |
| // run async | |
| .run({ 'async': true }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment