]> source.dussan.org Git - jquery.git/commitdiff
Improve speed of $.map with object support (-5% previous speed) and improve .length...
authorDan Heberden <danheberden@gmail.com>
Tue, 5 Apr 2011 06:59:54 +0000 (23:59 -0700)
committerDan Heberden <danheberden@gmail.com>
Tue, 5 Apr 2011 06:59:54 +0000 (23:59 -0700)
src/core.js
src/sizzle
test/qunit
test/unit/core.js

index 49c191b9cd22a5aa5bb2bf954e234b67af369e80..c9b560ae3cc3e8da32c697248339947ee6053f08 100644 (file)
@@ -704,16 +704,15 @@ jQuery.extend({
                return ret;
        },
 
-               // arg is for internal usage only
+       // arg is for internal usage only
        map: function( elems, callback, arg ) {
                var value, ret = [],
-                       i = 0,          
+                       i = 0,
                        length = elems.length,
-                       // process .length if it's just an object member
-                       isArray = length !== undefined && ( elems[ length - 1 ] || jQuery.isArray( elems ) );
-               
+                       // jquery objects are treated as arrays
+                       isArray = elems instanceof jQuery || length !== undefined && typeof length === "number" && ( ( length > 0 && elems[ 0 ] && elems[ length -1 ] ) || jQuery.isArray( elems ) ) ;
+
                // Go through the array, translating each of the items to their
-               // new value (or values).
                if ( isArray ) {
                        for ( ; i < length; i++ ) {
                                value = callback( elems[ i ], i, arg );
@@ -723,8 +722,8 @@ jQuery.extend({
                                }
                        }
 
-               // Go thorugh every key on the object, 
-               } else {                
+               // Go thorugh every key on the object,
+               } else {
                        for ( key in elems ) {
                                value = callback( elems[ key ], key, arg );
 
index ef19279f54ba49242c6461d47577c703f4f4e80e..f12b9309269ba7e705a99efe099f86ed1fe98d58 160000 (submodule)
@@ -1 +1 @@
-Subproject commit ef19279f54ba49242c6461d47577c703f4f4e80e
+Subproject commit f12b9309269ba7e705a99efe099f86ed1fe98d58
index cc8460c7b44f023c4f84ab1810b72bf6c6ee4542..d404faf8f587fcbe6b8907943022e6318dd51e0c 160000 (submodule)
@@ -1 +1 @@
-Subproject commit cc8460c7b44f023c4f84ab1810b72bf6c6ee4542
+Subproject commit d404faf8f587fcbe6b8907943022e6318dd51e0c
index 08d80400c7cdefb88261c8ef6df0c44f60475f0e..c15884fc60cef14383a440a77c257b5dfa97c580 100644 (file)
@@ -625,7 +625,7 @@ test("map()", function() {
                q("ap","ap","ap"),
                "Single Map"
        );
-       
+
        //for #2616
        var keys = jQuery.map( {a:1,b:2}, function( v, k ){
                return k;
@@ -636,7 +636,7 @@ test("map()", function() {
                return v;
        });
        equals( values.join(""), "12", "Map the values from a hash to an array" );
-       
+
        // object with length prop
        var values = jQuery.map( {a:1,b:2, length:3}, function( v, k ){
                return v;