Revision: 4126
Author: [email protected]
Date: Mon Mar 15 03:14:59 2010
Log: Optimized Array.prototype.{lastIndexOf,indexOf} by special casing comparison to undefined.

Review URL: http://codereview.chromium.org/851008
http://code.google.com/p/v8/source/detail?r=4126

Modified:
 /branches/bleeding_edge/src/array.js

=======================================
--- /branches/bleeding_edge/src/array.js        Fri Mar 12 10:27:31 2010
+++ /branches/bleeding_edge/src/array.js        Mon Mar 15 03:14:59 2010
@@ -994,11 +994,16 @@
     // If index is still negative, search the entire array.
     if (index < 0) index = 0;
   }
+  if (!IS_UNDEFINED(element)) {
+    for (var i = index; i < length; i++) {
+      if (this[i] === element) return i;
+    }
+    return -1;
+  }
   // Lookup through the array.
   for (var i = index; i < length; i++) {
-    var current = this[i];
-    if (!IS_UNDEFINED(current) || i in this) {
-      if (current === element) return i;
+    if (IS_UNDEFINED(this[i]) && i in this) {
+      return i;
     }
   }
   return -1;
@@ -1018,10 +1023,15 @@
     else if (index >= length) index = length - 1;
   }
   // Lookup through the array.
+  if (!IS_UNDEFINED(element)) {
+    for (var i = index; i >= 0; i--) {
+      if (this[i] === element) return i;
+    }
+    return -1;
+  }
   for (var i = index; i >= 0; i--) {
-    var current = this[i];
-    if (!IS_UNDEFINED(current) || i in this) {
-      if (current === element) return i;
+    if (IS_UNDEFINED(this[i]) && i in this) {
+      return i;
     }
   }
   return -1;

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to