refact(es6): use es6 syntax.
* fixed #10, But iojs-2 not support Object.assign, so use object-assign shim instead for now. * use es6 syntax. * use eslint to instead jshint.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
var Benchmark = require('benchmark');
|
||||
var benchmarks = require('beautify-benchmark');
|
||||
var util = require('util');
|
||||
'use strict';
|
||||
|
||||
var suite = new Benchmark.Suite();
|
||||
const Benchmark = require('benchmark');
|
||||
const benchmarks = require('beautify-benchmark');
|
||||
const util = require('util');
|
||||
|
||||
const suite = new Benchmark.Suite();
|
||||
|
||||
function normal(text) {
|
||||
if (arguments.length === 2) {
|
||||
@@ -17,13 +19,13 @@ function normal(text) {
|
||||
}
|
||||
|
||||
function apply() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
const args = Array.prototype.slice.call(arguments);
|
||||
return util.format.apply(util, args);
|
||||
}
|
||||
|
||||
function apply2() {
|
||||
var args = new Array(arguments.length);
|
||||
for (var i = 0, l = arguments.length; i < l; i++) {
|
||||
const args = new Array(arguments.length);
|
||||
for (let i = 0, l = arguments.length; i < l; i++) {
|
||||
args[i] = arguments[i];
|
||||
}
|
||||
return util.format.apply(util, args);
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
var Benchmark = require('benchmark');
|
||||
var benchmarks = require('beautify-benchmark');
|
||||
'use strict';
|
||||
|
||||
var suite = new Benchmark.Suite();
|
||||
const Benchmark = require('benchmark');
|
||||
const benchmarks = require('beautify-benchmark');
|
||||
|
||||
const suite = new Benchmark.Suite();
|
||||
|
||||
function slice() {
|
||||
return Array.prototype.slice.call(arguments);
|
||||
@@ -12,8 +14,8 @@ function slice0() {
|
||||
}
|
||||
|
||||
function forLoop() {
|
||||
var args = new Array(arguments.length);
|
||||
for(var i = 0; i < args.length; i++) {
|
||||
const args = new Array(arguments.length);
|
||||
for(let i = 0; i < args.length; i++) {
|
||||
args[i] = arguments[i];
|
||||
}
|
||||
return args;
|
||||
@@ -31,7 +33,7 @@ suite
|
||||
.add('Array.prototype.slice.call(arguments, 0)', function() {
|
||||
slice0(0, 1, 2, 3, 4, 5, 6, 7);
|
||||
})
|
||||
.add('for(var i = 0; i < args.length; i++) {}', function() {
|
||||
.add('for(let i = 0; i < args.length; i++) {}', function() {
|
||||
forLoop(0, 1, 2, 3, 4, 5, 6, 7);
|
||||
})
|
||||
|
||||
@@ -58,4 +60,4 @@ suite
|
||||
//
|
||||
// Array.prototype.slice.call(arguments) x 4,537,649 ops/sec ±1.18% (94 runs sampled)
|
||||
// Array.prototype.slice.call(arguments, 0) x 4,605,132 ops/sec ±0.87% (96 runs sampled)
|
||||
// for(var i = 0; i < args.length; i++) {} x 30,435,436 ops/sec ±0.91% (93 runs sampled)
|
||||
// for(let i = 0; i < args.length; i++) {} x 30,435,436 ops/sec ±0.91% (93 runs sampled)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
var Benchmark = require('benchmark');
|
||||
var benchmarks = require('beautify-benchmark');
|
||||
'use strict';
|
||||
|
||||
var suite = new Benchmark.Suite();
|
||||
const Benchmark = require('benchmark');
|
||||
const benchmarks = require('beautify-benchmark');
|
||||
|
||||
const suite = new Benchmark.Suite();
|
||||
|
||||
function endsWith(str) {
|
||||
return str.endsWith('.properties');
|
||||
|
||||
203
benchmark/flattening.js
Normal file
203
benchmark/flattening.js
Normal file
@@ -0,0 +1,203 @@
|
||||
'use strict';
|
||||
|
||||
const Benchmark = require('benchmark');
|
||||
const benchmarks = require('beautify-benchmark');
|
||||
|
||||
const suite = new Benchmark.Suite();
|
||||
|
||||
function isObject(obj) {
|
||||
return Object.prototype.toString.call(obj) === '[object Object]';
|
||||
}
|
||||
|
||||
function flattening(data) {
|
||||
|
||||
const result = {};
|
||||
|
||||
function deepFlat (data, keys) {
|
||||
Object.keys(data).forEach(function(key) {
|
||||
const value = data[key];
|
||||
const k = keys ? key : keys + '.' + key;
|
||||
if (!isObject(value)) {
|
||||
return result[k] = String(value);
|
||||
}
|
||||
deepFlat(value, k);
|
||||
});
|
||||
}
|
||||
|
||||
deepFlat(data, '');
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function flattening_1(data) {
|
||||
|
||||
const result = {};
|
||||
|
||||
function deepFlat (data, keys) {
|
||||
Object.keys(data).forEach(function(key) {
|
||||
const value = data[key];
|
||||
const k = keys.concat(key);
|
||||
if (isObject(value)) {
|
||||
deepFlat(value, k);
|
||||
} else {
|
||||
result[k.join('.')] = String(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
deepFlat(data, []);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function flattening_2(data) {
|
||||
|
||||
const result = {};
|
||||
|
||||
function deepFlat (data, flatKey, key) {
|
||||
const value = data[key];
|
||||
if (isObject(value)) {
|
||||
Object.keys(value).forEach(function(k) {
|
||||
deepFlat(value, flatKey + '.' + k, k);
|
||||
});
|
||||
} else {
|
||||
result[flatKey] = String(value);
|
||||
}
|
||||
}
|
||||
|
||||
Object.keys(data).forEach(function(key) {
|
||||
deepFlat(data, key, key);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
const resource = {
|
||||
'model.user.foo.bar.aa': 'Hello',
|
||||
model: {
|
||||
user: {
|
||||
fields: {
|
||||
name: 'Real Name',
|
||||
age: 'Age',
|
||||
a: {
|
||||
b: {
|
||||
c: {
|
||||
d: {
|
||||
e: {
|
||||
f: 'fff',
|
||||
},
|
||||
},
|
||||
model: {
|
||||
user: {
|
||||
fields: {
|
||||
name: 'Real Name',
|
||||
age: 'Age',
|
||||
a: {
|
||||
b: {
|
||||
c: {
|
||||
d: {
|
||||
e: {
|
||||
f: 'fff',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
post: {
|
||||
fields: {
|
||||
title: 'Subject',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
model: {
|
||||
user: {
|
||||
fields: {
|
||||
name: 'Real Name',
|
||||
age: 'Age',
|
||||
a: {
|
||||
b: {
|
||||
c: {
|
||||
d: {
|
||||
e: {
|
||||
f: 'fff',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
post: {
|
||||
fields: {
|
||||
title: 'Subject',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
post: {
|
||||
fields: {
|
||||
title: 'Subject',
|
||||
},
|
||||
},
|
||||
model: {
|
||||
user: {
|
||||
fields: {
|
||||
name: 'Real Name',
|
||||
age: 'Age',
|
||||
a: {
|
||||
b: {
|
||||
c: {
|
||||
d: {
|
||||
e: {
|
||||
f: 'fff',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
post: {
|
||||
fields: {
|
||||
title: 'Subject',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
//console.log('flattening:', flattening(resource));
|
||||
//console.log('flattening_1:', flattening_1(resource));
|
||||
//console.log('flattening_2:', flattening_2(resource));
|
||||
|
||||
suite
|
||||
|
||||
.add('flattening', function() {
|
||||
flattening(resource);
|
||||
})
|
||||
.add('flattening_1', function() {
|
||||
flattening_1(resource);
|
||||
})
|
||||
.add('flattening_2', function() {
|
||||
flattening_2(resource);
|
||||
})
|
||||
.on('cycle', function(event) {
|
||||
benchmarks.add(event.target);
|
||||
})
|
||||
.on('complete', function done() {
|
||||
benchmarks.log();
|
||||
})
|
||||
.run({ async: false });
|
||||
|
||||
//$ node benchmark/flattening.js
|
||||
//
|
||||
// 3 tests completed.
|
||||
//
|
||||
// flattening x 32,863 ops/sec ±0.83% (98 runs sampled)
|
||||
// flattening_1 x 10,434 ops/sec ±0.73% (96 runs sampled)
|
||||
// flattening_2 x 21,734 ops/sec ±1.04% (95 runs sampled)
|
||||
@@ -1,17 +1,19 @@
|
||||
var Benchmark = require('benchmark');
|
||||
var benchmarks = require('beautify-benchmark');
|
||||
'use strict';
|
||||
|
||||
var suite = new Benchmark.Suite();
|
||||
const Benchmark = require('benchmark');
|
||||
const benchmarks = require('beautify-benchmark');
|
||||
|
||||
const suite = new Benchmark.Suite();
|
||||
|
||||
function getNestedValue(data, key) {
|
||||
var keys = key.split('.');
|
||||
for (var i = 0; typeof data === 'object' && i < keys.length; i++) {
|
||||
const keys = key.split('.');
|
||||
for (let i = 0; typeof data === 'object' && i < keys.length; i++) {
|
||||
data = data[keys[i]];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
var resource = {
|
||||
const resource = {
|
||||
'model.user.foo.bar.aa': 'Hello',
|
||||
model: {
|
||||
user: {
|
||||
@@ -23,8 +25,8 @@ var resource = {
|
||||
c: {
|
||||
d: {
|
||||
e: {
|
||||
f: "fff"
|
||||
}
|
||||
f: 'fff',
|
||||
},
|
||||
},
|
||||
model: {
|
||||
user: {
|
||||
@@ -36,23 +38,23 @@ var resource = {
|
||||
c: {
|
||||
d: {
|
||||
e: {
|
||||
f: "fff"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
f: 'fff',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
post: {
|
||||
fields: {
|
||||
title: 'Subject'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
title: 'Subject',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
model: {
|
||||
user: {
|
||||
@@ -64,25 +66,25 @@ var resource = {
|
||||
c: {
|
||||
d: {
|
||||
e: {
|
||||
f: "fff"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
f: 'fff',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
post: {
|
||||
fields: {
|
||||
title: 'Subject'
|
||||
}
|
||||
}
|
||||
}
|
||||
title: 'Subject',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
post: {
|
||||
fields: {
|
||||
title: 'Subject'
|
||||
}
|
||||
title: 'Subject',
|
||||
},
|
||||
},
|
||||
model: {
|
||||
user: {
|
||||
@@ -94,24 +96,24 @@ var resource = {
|
||||
c: {
|
||||
d: {
|
||||
e: {
|
||||
f: "fff"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
f: 'fff',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
post: {
|
||||
fields: {
|
||||
title: 'Subject'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
title: 'Subject',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
var fullKey = 'model.user.fields.a.b.c.d.e.f';
|
||||
const fullKey = 'model.user.fields.a.b.c.d.e.f';
|
||||
|
||||
console.log('Deeps: ', fullKey.split('.').length);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user