Browse Source

if and return series suggested by mrdoob here: https://github.com/bhouston/three.js/commit/6c765dcdb2c790ddcc5d613e96be8a0e66eca1f8#commitcomment-2346419

Ben Houston 12 years ago
parent
commit
eb25ee7690
1 changed files with 44 additions and 0 deletions
  1. 44 0
      test/benchmark/core/Vector3Components.js

+ 44 - 0
test/benchmark/core/Vector3Components.js

@@ -51,6 +51,26 @@ THREE.Vector3.prototype = {
         }
 
     },
+
+
+    getComponent3: function ( index ) {
+
+        if ( index === 0 ) return this.x;
+        if ( index === 1 ) return this.y;
+        if ( index === 2 ) return this.z;
+
+        throw new Error( "index is out of range: " + index );
+
+    },
+
+    getComponent4: function ( index ) {
+
+        if ( index === 0 ) return this.x;
+        else if ( index === 1 ) return this.y;
+        else if ( index === 2 ) return this.z;
+        else throw new Error( "index is out of range: " + index );
+
+    }
 };
 
 
@@ -93,6 +113,30 @@ suite.add('SwitchStatement', function() {
 
 });
 
+suite.add('IfAndReturnSeries', function() {
+
+    var result = 0;
+
+    for ( var i = 0; i < 100000; i ++ ) {
+
+        result += a[i].getComponent3( i % 3 );
+
+    }
+
+});
+
+suite.add('IfReturnElseSeries', function() {
+
+    var result = 0;
+
+    for ( var i = 0; i < 100000; i ++ ) {
+
+        result += a[i].getComponent4( i % 3 );
+
+    }
+
+});
+
 suite.on('cycle', function(event, bench) {
   console.log(String(event.target));
 });