forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector3Components.js
110 lines (89 loc) · 2.28 KB
/
Vector3Components.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(function() {
var s = Bench.newSuite("Vector 3 Components");
THREE = {};
THREE.Vector3 = function(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
};
THREE.Vector3.prototype = {
constructor: THREE.Vector3,
setComponent: function(index, value) {
this[THREE.Vector3.__indexToName[index]] = value;
},
getComponent: function(index) {
return this[THREE.Vector3.__indexToName[index]];
},
setComponent2: function(index, value) {
switch (index) {
case 0:
this.x = value;
break;
case 1:
this.y = value;
break;
case 2:
this.z = value;
break;
default:
throw new Error("index is out of range: " + index);
}
},
getComponent2: function(index) {
switch (index) {
case 0:
return this.x;
case 1:
return this.y;
case 2:
return this.z;
default:
throw new Error("index is out of range: " + index);
}
},
getComponent3: function(index) {
if (index === 0) return this.x;
if (index === 1) return this.y;
if (index === 2) return this.z;
throw new Error("index is out of range: " + index);
},
getComponent4: function(index) {
if (index === 0) return this.x;else if (index === 1) return this.y;else if (index === 2) return this.z;
else
throw new Error("index is out of range: " + index);
}
};
THREE.Vector3.__indexToName = {
0: 'x',
1: 'y',
2: 'z'
};
var a = [];
for (var i = 0; i < 100000; i++) {
a[i] = new THREE.Vector3(i * 0.01, i * 2, i * -1.3);
}
s.add('IndexToName', function() {
var result = 0;
for (var i = 0; i < 100000; i++) {
result += a[i].getComponent(i % 3);
}
});
s.add('SwitchStatement', function() {
var result = 0;
for (var i = 0; i < 100000; i++) {
result += a[i].getComponent2(i % 3);
}
});
s.add('IfAndReturnSeries', function() {
var result = 0;
for (var i = 0; i < 100000; i++) {
result += a[i].getComponent3(i % 3);
}
});
s.add('IfReturnElseSeries', function() {
var result = 0;
for (var i = 0; i < 100000; i++) {
result += a[i].getComponent4(i % 3);
}
});
})();