Skip to content

Instantly share code, notes, and snippets.

@Aschen
Last active November 29, 2019 04:11
Show Gist options
  • Select an option

  • Save Aschen/f1c790fbfa0a8825bccd4fdb75f3becf to your computer and use it in GitHub Desktop.

Select an option

Save Aschen/f1c790fbfa0a8825bccd4fdb75f3becf to your computer and use it in GitHub Desktop.
Benchmarking object destructuring
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();
$ 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