Skip to content

Commit 86e51b9

Browse files
committed
Code refactoring
1 parent 26dabe0 commit 86e51b9

14 files changed

+130
-223
lines changed

JavaScript/1-console-time.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
const LOOP_COUNT = 50000;
44

5-
function fn() {
5+
const fn = () => {
66
const a = [];
7-
let i;
8-
for (i = 0; i < LOOP_COUNT; i++) {
7+
for (let i = 0; i < LOOP_COUNT; i++) {
98
a.push(Array(i).join('A').length);
109
}
1110
return a;
12-
}
11+
};
1312

1413
console.time('experiment');
1514
const res1 = fn();

JavaScript/2-benchmark.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,35 @@ benchmark.do = (count, tests) => {
5555
const times = tests.map(fn => {
5656
if (global.gc) gc();
5757
const result = [];
58-
let i;
5958
const optBefore = opt(fn);
6059
optimize(fn);
6160
fn();
6261
const optAfter = opt(fn);
63-
for (i = 0; i < PRE_COUNT; i++) result.push(fn());
62+
for (let i = 0; i < PRE_COUNT; i++) result.push(fn());
6463
const optAfterHeat = opt(fn);
6564
const begin = process.hrtime();
66-
for (i = 0; i < count; i++) result.push(fn());
65+
for (let i = 0; i < count; i++) result.push(fn());
6766
const end = process.hrtime(begin);
6867
const optAfterLoop = opt(fn);
6968
const diff = end[0] * 1e9 + end[1];
70-
const time = lpad(diff.toString(), '.', 15);
69+
const time = diff.toString();
7170
const name = rpad(fn.name, '.', 25);
7271
const iterations = result.length - PRE_COUNT;
73-
console.log(
74-
`${name}${time} ${optBefore} ${optAfter} ${optAfterHeat} ${optAfterLoop}`
75-
);
72+
const log = [
73+
name, time, optBefore, optAfter,
74+
optAfterHeat, optAfterLoop
75+
];
76+
console.log(log.join(' '));
7677
return { name, time: diff };
7778
});
7879
console.log();
7980
const top = times.sort((t1, t2) => (t1.time - t2.time));
8081
const best = top[0].time;
81-
top.forEach((test) => {
82+
top.forEach(test => {
8283
test.percent = relativePercent(best, test.time);
8384
const time = lpad(test.time.toString(), '.', 15);
84-
const percent = lpad((
85-
test.percent === 0 ? 'min' : '+' + test.percent + '%'
86-
), '.', 10);
87-
console.log(test.name + time + percent);
85+
const percent = test.percent === 0 ? 'min' : `+${test.percent}%`;
86+
const line = lpad(percent, '.', 10);
87+
console.log(test.name + time + line);
8888
});
8989
};

JavaScript/3-instantiation.js

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,39 @@
22

33
const benchmark = require('./2-benchmark.js');
44

5-
function makeClosure(hello, size, flag) {
6-
return () => ({ hello, size, flag });
7-
// try {} catch (e) {}
8-
}
5+
const makeClosure = (hello, size, flag) => () => (
6+
{ hello, size, flag }
7+
);
98

10-
function closureInstance() {
11-
return makeClosure('world', 100500, true);
12-
}
9+
const closureInstance = () => makeClosure('world', 100500, true);
1310

14-
function defineArray() {
15-
return ['world', 100500, true];
16-
}
11+
const defineArray = () => ['world', 100500, true];
1712

18-
function defineArrayOfString() {
19-
return ['world', 'world', 'world'];
20-
}
13+
const defineArrayOfString = () => ['world', 'world', 'world'];
2114

22-
function defineArrayOfNumber() {
23-
return [100500, 100500, 100500];
24-
}
15+
const defineArrayOfNumber = () => [100500, 100500, 100500];
2516

26-
function defineObject() {
27-
return {
28-
hello: 'world',
29-
size: 100500,
30-
flag: true
31-
};
32-
}
17+
const defineObject = () => ({
18+
hello: 'world',
19+
size: 100500,
20+
flag: true
21+
});
3322

34-
function mixinObject() {
23+
const mixinObject = () => {
3524
const obj = {};
3625
obj.hello = 'world';
3726
obj.size = 100500;
3827
obj.flag = true;
3928
return obj;
40-
}
29+
};
4130

4231
function ProtoItem(hello, size, flag) {
4332
this.hello = hello;
4433
this.size = size;
4534
this.flag = flag;
4635
}
4736

48-
function newPrototype() {
49-
return new ProtoItem('world', 100500, true);
50-
}
37+
const newPrototype = () => new ProtoItem('world', 100500, true);
5138

5239
const ClassItem = class {
5340
constructor(hello, size, flag) {
@@ -57,33 +44,27 @@ const ClassItem = class {
5744
}
5845
};
5946

60-
function newClass() {
61-
return new ClassItem('world', 100500, true);
62-
}
47+
const newClass = () => new ClassItem('world', 100500, true);
6348

64-
function newObject() {
49+
const newObject = () => {
6550
const obj = new Object();
6651
obj.hello = 'world';
6752
obj.size = 100500;
6853
obj.flag = true;
6954
return obj;
70-
}
55+
};
7156

72-
function objectCreate() {
73-
const obj = Object.create(objectCreate.prototype);
57+
const objectCreate = () => {
58+
const obj = Object.create(null);
7459
obj.hello = 'world';
7560
obj.size = 100500;
7661
obj.flag = true;
7762
return obj;
78-
}
63+
};
7964

80-
function callFactory() {
81-
return itemFactory('world', 100500, true);
82-
}
65+
const itemFactory = (hello, size, flag) => ({ hello, size, flag });
8366

84-
function itemFactory(hello, size, flag) {
85-
return { hello, size, flag };
86-
}
67+
const callFactory = () => itemFactory('world', 100500, true);
8768

8869
benchmark.do(1000000, [
8970
callFactory,

JavaScript/4-array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function buildGetter(proto, fieldName, fieldType, fieldIndex) {
5656
return new Date(this[fieldIndex]);
5757
}
5858
});
59-
} else if (typeof(fieldType) === 'function') {
59+
} else if (typeof fieldType === 'function') {
6060
Object.defineProperty(proto, fieldName, { get: fieldType });
6161
} else {
6262
Object.defineProperty(proto, fieldName, {

JavaScript/5-range.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,47 @@
22

33
const benchmark = require('./2-benchmark.js');
44

5-
function rangePush(min, max) {
5+
const rangePush = (min, max) => {
66
const arr = [];
7-
let i;
8-
for (i = min; i <= max; i++) arr.push(i);
7+
for (let i = min; i <= max; i++) arr.push(i);
98
return arr;
10-
}
9+
};
1110

12-
function rangeNew(from, to) {
11+
const rangeNew = (from, to) => {
1312
if (to < from) return [];
1413
const len = to - from + 1;
1514
const range = new Array(len);
16-
let i;
17-
for (i = from; i <= to; i++) {
15+
for (let i = from; i <= to; i++) {
1816
range[i - from] = i;
1917
}
2018
return range;
21-
}
19+
};
2220

23-
function rangeEx(range) {
21+
const rangeEx = range => {
2422
const from = range[0];
2523
let to = range[1];
26-
const toType = typeof(to);
27-
let count, res;
24+
const toType = typeof to;
2825
if (toType === 'undefined') {
2926
to = range[2];
30-
res = api.common.range(from, to);
27+
return api.common.range(from, to);
3128
} else if (toType !== 'number') {
32-
count = to[0];
29+
let count = to[0];
3330
if (count < 0) {
34-
cpus = api.os.cpus().length;
31+
const cpus = api.os.cpus().length;
3532
count = cpus + count;
3633
}
37-
range = api.common.range(from, from + count - 1);
34+
return api.common.range(from, from + count - 1);
3835
}
39-
return res;
40-
}
36+
};
4137

4238
benchmark.do(1000000, [
4339
function testRangePush() {
4440
rangePush(1, 1000);
4541
},
4642
function testRangeNew() {
4743
rangeNew(1, 1000);
48-
}
44+
},
45+
function testRangeEx() {
46+
rangeNew(1, 1000);
47+
},
4948
]);

JavaScript/6-functions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
const benchmark = require('./2-benchmark.js');
44

55
const fnLambdaBlock = () => {};
6+
// eslint-disable-next-line
67
const fnLambdaBlockU = () => { return; };
8+
// eslint-disable-next-line
79
const fnLambdaBlockN = () => { return null; };
10+
// eslint-disable-next-line
811
const fnLambdaBlock0 = () => { return 0; };
912

1013
const fnLambdaExprU = () => undefined;

JavaScript/9-for-in.js

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,62 +12,35 @@ const data = {
1212
yz: 'xyz'
1313
};
1414

15-
function testForKeys() {
15+
const testForKeys = () => {
1616
const a = Array(7);
17-
let i, key;
1817
const keys = Object.keys(data);
19-
const len = keys.length;
20-
for (i = 0; i < len; i++) {
21-
key = keys[i];
18+
for (let i = 0; i < keys.length; i++) {
19+
const key = keys[i];
2220
a[i] = data[key];
2321
}
24-
}
25-
26-
function testForIn() {
27-
const a = Array(7);
28-
let i = 0;
29-
for (let key in data) {
30-
a[i++] = data[key];
31-
}
32-
}
22+
};
3323

34-
function testForInLet() {
24+
const testForIn = () => {
3525
const a = Array(7);
3626
let i = 0;
37-
let key;
38-
for (key in data) {
27+
for (const key in data) {
3928
a[i++] = data[key];
4029
}
41-
}
42-
43-
function testForOf() {
44-
const a = Array(7);
45-
let i = 0;
46-
let key, val;
47-
const keys = Object.keys(data);
48-
const len = keys.length;
49-
for (key of keys) {
50-
val = data[key];
51-
a[i++] = val;
52-
}
53-
}
30+
};
5431

55-
function testForOfLet() {
32+
const testForOf = () => {
5633
const a = Array(7);
5734
let i = 0;
58-
let key, val;
5935
const keys = Object.keys(data);
60-
const len = keys.length;
61-
for (let key of keys) {
62-
val = data[key];
36+
for (const key of keys) {
37+
const val = data[key];
6338
a[i++] = val;
6439
}
65-
}
40+
};
6641

67-
benchmark.do(1000000, [
42+
benchmark.do(10000000, [
6843
testForKeys,
6944
testForIn,
70-
testForInLet,
7145
testForOf,
72-
testForOfLet
7346
]);

JavaScript/a-tick-template.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

JavaScript/a-tick.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const benchmark = require('./2-benchmark.js');
4+
5+
const testConcat = () => 'Hello user' + parseInt('5') * 10 + ' !';
6+
7+
const testTick = () => `Hello user${parseInt('5') * 10} !`;
8+
9+
benchmark.do(10000000, [
10+
testConcat,
11+
testTick,
12+
]);

0 commit comments

Comments
 (0)