Vector3Length.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ( function () {
  2. var THREE = {};
  3. THREE.Vector3 = function ( x, y, z ) {
  4. this.x = x || 0;
  5. this.y = y || 0;
  6. this.z = z || 0;
  7. };
  8. THREE.Vector3.prototype = {
  9. constructor: THREE.Vector3,
  10. lengthSq: function () {
  11. return this.x * this.x + this.y * this.y + this.z * this.z;
  12. },
  13. length: function () {
  14. return Math.sqrt( this.lengthSq() );
  15. },
  16. length2: function () {
  17. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  18. }
  19. };
  20. var a = [];
  21. for ( var i = 0; i < 100000; i ++ ) {
  22. a[ i ] = new THREE.Vector3( i * 0.01, i * 2, i * - 1.3 );
  23. }
  24. var suite = Bench.newSuite( 'Vector 3 Length' );
  25. suite.add( 'NoCallTest', function () {
  26. var result = 0;
  27. for ( var i = 0; i < 100000; i ++ ) {
  28. var v = a[ i ];
  29. result += Math.sqrt( v.x * v.x + v.y * v.y + v.z * v.z );
  30. }
  31. } );
  32. suite.add( 'InlineCallTest', function () {
  33. var result = 0;
  34. for ( var i = 0; i < 100000; i ++ ) {
  35. result += a[ i ].length2();
  36. }
  37. } );
  38. suite.add( 'FunctionCallTest', function () {
  39. var result = 0;
  40. for ( var i = 0; i < 100000; i ++ ) {
  41. result += a[ i ].length();
  42. }
  43. } );
  44. } )();