Vector3Components.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. (function() {
  2. var s = Bench.newSuite("Vector 3 Components");
  3. THREE = {};
  4. THREE.Vector3 = function(x, y, z) {
  5. this.x = x || 0;
  6. this.y = y || 0;
  7. this.z = z || 0;
  8. };
  9. THREE.Vector3.prototype = {
  10. constructor: THREE.Vector3,
  11. setComponent: function(index, value) {
  12. this[THREE.Vector3.__indexToName[index]] = value;
  13. },
  14. getComponent: function(index) {
  15. return this[THREE.Vector3.__indexToName[index]];
  16. },
  17. setComponent2: function(index, value) {
  18. switch (index) {
  19. case 0:
  20. this.x = value;
  21. break;
  22. case 1:
  23. this.y = value;
  24. break;
  25. case 2:
  26. this.z = value;
  27. break;
  28. default:
  29. throw new Error("index is out of range: " + index);
  30. }
  31. },
  32. getComponent2: function(index) {
  33. switch (index) {
  34. case 0:
  35. return this.x;
  36. case 1:
  37. return this.y;
  38. case 2:
  39. return this.z;
  40. default:
  41. throw new Error("index is out of range: " + index);
  42. }
  43. },
  44. getComponent3: function(index) {
  45. if (index === 0) return this.x;
  46. if (index === 1) return this.y;
  47. if (index === 2) return this.z;
  48. throw new Error("index is out of range: " + index);
  49. },
  50. getComponent4: function(index) {
  51. if (index === 0) return this.x;else if (index === 1) return this.y;else if (index === 2) return this.z;
  52. else
  53. throw new Error("index is out of range: " + index);
  54. }
  55. };
  56. THREE.Vector3.__indexToName = {
  57. 0: 'x',
  58. 1: 'y',
  59. 2: 'z'
  60. };
  61. var a = [];
  62. for (var i = 0; i < 100000; i++) {
  63. a[i] = new THREE.Vector3(i * 0.01, i * 2, i * -1.3);
  64. }
  65. s.add('IndexToName', function() {
  66. var result = 0;
  67. for (var i = 0; i < 100000; i++) {
  68. result += a[i].getComponent(i % 3);
  69. }
  70. });
  71. s.add('SwitchStatement', function() {
  72. var result = 0;
  73. for (var i = 0; i < 100000; i++) {
  74. result += a[i].getComponent2(i % 3);
  75. }
  76. });
  77. s.add('IfAndReturnSeries', function() {
  78. var result = 0;
  79. for (var i = 0; i < 100000; i++) {
  80. result += a[i].getComponent3(i % 3);
  81. }
  82. });
  83. s.add('IfReturnElseSeries', function() {
  84. var result = 0;
  85. for (var i = 0; i < 100000; i++) {
  86. result += a[i].getComponent4(i % 3);
  87. }
  88. });
  89. })();