Skip to content

Instantly share code, notes, and snippets.

@dead-horse
Created April 29, 2016 03:11
Show Gist options
  • Save dead-horse/98e013552bf60c66d3db5164843fa40b to your computer and use it in GitHub Desktop.
Save dead-horse/98e013552bf60c66d3db5164843fa40b to your computer and use it in GitHub Desktop.
assgin_for_in_of
'use strict';
const Benchmark = require('benchmark');
const benchmarks = require('beautify-benchmark');
const suite = new Benchmark.Suite();
suite
.add('Object.assign', function() {
const a = {};
const b = { a: 1, b: 2, c: 3, d: 4, e: 5};
Object.assign(a, b);
})
.add('for in', function() {
const a = {};
const b = { a: 1, b: 2, c: 3, d: 4, e: 5};
for (let key in b) {
a[key] = b[key];
}
})
.add('Object.keys', function() {
const a = {};
const b = { a: 1, b: 2, c: 3, d: 4, e: 5};
const keys = Object.keys(b);
for (var i = 0; i < keys.length; i++) {
const key = keys[i];
a[key] = b[key];
}
})
.add('of Object.keys', function() {
const a = {};
const b = { a: 1, b: 2, c: 3, d: 4, e: 5};
const keys = Object.keys(b);
for (const key of keys) {
a[key] = b[key];
}
})
.on('cycle', function (event) {
benchmarks.add(event.target);
})
.on('start', function () {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', function() {
benchmarks.log();
process.exit(0);
})
.run({ 'async': false });
// node version: v4.4.2, date: Fri Apr 29 2016 11:09:34 GMT+0800 (CST)
// Starting...
// 4 tests completed.
// Object.assign x 637,618 ops/sec ±1.59% (87 runs sampled)
// for in x 2,576,420 ops/sec ±1.21% (90 runs sampled)
// Object.keys x 3,087,945 ops/sec ±1.46% (86 runs sampled)
// of Object.keys x 874,902 ops/sec ±2.71% (86 runs sampled)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment