Vector3Components.js 2.6 KB

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