-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path3-instantiation.js
More file actions
81 lines (64 loc) · 1.55 KB
/
3-instantiation.js
File metadata and controls
81 lines (64 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
const benchmark = require('./2-benchmark.js');
const makeClosure = (hello, size, flag) => () => (
{ hello, size, flag }
);
const closureInstance = () => makeClosure('world', 100500, true);
const defineArray = () => ['world', 100500, true];
const defineArrayOfString = () => ['world', 'world', 'world'];
const defineArrayOfNumber = () => [100500, 100500, 100500];
const defineObject = () => ({
hello: 'world',
size: 100500,
flag: true
});
const mixinObject = () => {
const obj = {};
obj.hello = 'world';
obj.size = 100500;
obj.flag = true;
return obj;
};
function ProtoItem(hello, size, flag) {
this.hello = hello;
this.size = size;
this.flag = flag;
}
const newPrototype = () => new ProtoItem('world', 100500, true);
const ClassItem = class {
constructor(hello, size, flag) {
this.hello = hello;
this.size = size;
this.flag = flag;
}
};
const newClass = () => new ClassItem('world', 100500, true);
const newObject = () => {
const obj = new Object();
obj.hello = 'world';
obj.size = 100500;
obj.flag = true;
return obj;
};
const objectCreate = () => {
const obj = Object.create(null);
obj.hello = 'world';
obj.size = 100500;
obj.flag = true;
return obj;
};
const itemFactory = (hello, size, flag) => ({ hello, size, flag });
const callFactory = () => itemFactory('world', 100500, true);
benchmark.do(1000000, [
callFactory,
closureInstance,
defineObject,
defineArray,
defineArrayOfString,
defineArrayOfNumber,
mixinObject,
newPrototype,
newClass,
newObject,
objectCreate
]);