Last active
November 29, 2019 04:11
-
-
Save Aschen/f1c790fbfa0a8825bccd4fdb75f3becf to your computer and use it in GitHub Desktop.
Benchmarking object destructuring
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 obj = { | |
| value: 42, | |
| name: 'jean', | |
| not: 'taked' | |
| }; | |
| const arr = [obj, Object.assign({}, obj), Object.assign({}, obj), Object.assign({}, obj)]; | |
| suite | |
| .add('access property', () => { | |
| for (const o of arr) { | |
| const res = { | |
| value: o.value, | |
| name: o.name | |
| } | |
| } | |
| }) | |
| .add('destructuring', () => { | |
| for (const o of arr) { | |
| const { value, name } = o; | |
| const res = { | |
| value, | |
| name | |
| } | |
| } | |
| }) | |
| .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 access-vs-destructuring.js | |
| access property x 102,225,644 ops/sec ±0.46% (90 runs sampled) | |
| destructuring x 64,626,511 ops/sec ±1.24% (95 runs sampled) | |
| Fastest is access property |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment