Skip to content

Commit a8d42ae

Browse files
committed
Add class instantiation example
1 parent 4ede8a1 commit a8d42ae

File tree

1 file changed

+38
-50
lines changed

1 file changed

+38
-50
lines changed

JavaScript/3-instantiation.js

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,18 @@
22

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

5-
benchmark.do(10000000, 5, [
6-
defineObject,
7-
defineArray,
8-
defineArrayOfString,
9-
defineArrayOfNumber,
10-
mixinObject,
11-
newInstance,
12-
newObject,
13-
objectCreate,
14-
callFactory
15-
]);
16-
175
function defineArray() {
18-
return [
19-
'world',
20-
100500,
21-
true
22-
];
6+
return ['world', 100500, true];
237
}
248

259
function defineArrayOfString() {
26-
return [
27-
'world',
28-
'world',
29-
'world'
30-
];
10+
return ['world', 'world', 'world'];
3111
}
3212

3313
function defineArrayOfNumber() {
34-
return [
35-
100500,
36-
100500,
37-
100500
38-
];
14+
return [100500, 100500, 100500];
3915
}
4016

41-
4217
function defineObject() {
4318
return {
4419
hello: 'world',
@@ -55,12 +30,26 @@ function mixinObject() {
5530
return obj;
5631
}
5732

58-
function newInstance() {
59-
return new Item(
60-
'world',
61-
100500,
62-
true
63-
);
33+
function ProtoItem(hello, size, flag) {
34+
this.hello = hello;
35+
this.size = size;
36+
this.flag = flag;
37+
}
38+
39+
function newPrototype() {
40+
return new ProtoItem('world', 100500, true);
41+
}
42+
43+
const ClassItem = class {
44+
constructor(hello, size, flag) {
45+
this.hello = hello;
46+
this.size = size;
47+
this.flag = flag;
48+
}
49+
};
50+
51+
function newClass() {
52+
return new ClassItem('world', 100500, true);
6453
}
6554

6655
function newObject() {
@@ -80,23 +69,22 @@ function objectCreate() {
8069
}
8170

8271
function callFactory() {
83-
return item(
84-
'world',
85-
100500,
86-
true
87-
);
72+
return itemFactory('world', 100500, true);
8873
}
8974

90-
function Item(hello, size, flag) {
91-
this.hello = hello;
92-
this.size = size;
93-
this.flag = flag;
75+
function itemFactory(hello, size, flag) {
76+
return { hello, size, flag };
9477
}
9578

96-
function item(hello, size, flag) {
97-
return {
98-
hello,
99-
size,
100-
flag
101-
};
102-
}
79+
benchmark.do(10000000, 5, [
80+
defineObject,
81+
defineArray,
82+
defineArrayOfString,
83+
defineArrayOfNumber,
84+
mixinObject,
85+
newPrototype,
86+
newClass,
87+
newObject,
88+
objectCreate,
89+
callFactory
90+
]);

0 commit comments

Comments
 (0)