2
0

Vector3Length.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. return result;
  32. } );
  33. suite.add( 'InlineCallTest', function () {
  34. var result = 0;
  35. for ( var i = 0; i < 100000; i ++ ) {
  36. result += a[ i ].length2();
  37. }
  38. return result;
  39. } );
  40. suite.add( 'FunctionCallTest', function () {
  41. var result = 0;
  42. for ( var i = 0; i < 100000; i ++ ) {
  43. result += a[ i ].length();
  44. }
  45. return result;
  46. } );
  47. } )();