mirror of
https://github.com/jquery/jquery.git
synced 2026-01-24 06:57:55 -05:00
Improve speed of $.map with object support (-5% previous speed) and improve .length detection
This commit is contained in:
15
src/core.js
15
src/core.js
@@ -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 );
|
||||
|
||||
|
||||
Submodule src/sizzle updated: ef19279f54...f12b930926
Submodule test/qunit updated: cc8460c7b4...d404faf8f5
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user