Vector3Components.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. return result;
  71. } );
  72. s.add( 'SwitchStatement', function () {
  73. var result = 0;
  74. for ( var i = 0; i < 100000; i ++ ) {
  75. result += a[ i ].getComponent2( i % 3 );
  76. }
  77. return result;
  78. } );
  79. s.add( 'IfAndReturnSeries', function () {
  80. var result = 0;
  81. for ( var i = 0; i < 100000; i ++ ) {
  82. result += a[ i ].getComponent3( i % 3 );
  83. }
  84. return result;
  85. } );
  86. s.add( 'IfReturnElseSeries', function () {
  87. var result = 0;
  88. for ( var i = 0; i < 100000; i ++ ) {
  89. result += a[ i ].getComponent4( i % 3 );
  90. }
  91. return result;
  92. } );
  93. } )();