Skip to content

Commit

Permalink
Storing object[ name ] in a local variable to save property lookup ti…
Browse files Browse the repository at this point in the history
…mes in jQuery.each when iterating over objects.
  • Loading branch information
fitzgen committed May 7, 2010
1 parent 1533bf7 commit 6e61c0a
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/core.js
Expand Up @@ -535,7 +535,7 @@ jQuery.extend({

// args is for internal usage only
each: function( object, callback, args ) {
var name, i = 0,
var name, value, i = 0,
length = object.length,
isObj = length === undefined || jQuery.isFunction(object);

Expand All @@ -558,12 +558,13 @@ jQuery.extend({
} else {
if ( isObj ) {
for ( name in object ) {
if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
value = object[ name ];
if ( callback.call( value, name, value ) === false ) {
break;
}
}
} else {
for ( var value = object[0];
for ( value = object[0];
i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
}
}
Expand Down

1 comment on commit 6e61c0a

@gf3
Copy link

@gf3 gf3 commented on 6e61c0a May 7, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure?

var obj = {lol: "NO U"};
→ undefined

console.time("lookup"); for (var i=0; i<100000; i++) { obj.lol, obj.lol } console.timeEnd("lookup");
lookup: 21ms
→ undefined

console.time("lookup2"); s = "lol"; for (var i=0; i<100000; i++) { obj[ s ], obj[ s ] } console.timeEnd("lookup2");
lookup2: 37ms
→ undefined

console.time("cache"); for (var i=0; i<100000; i++) { b = obj.lol, b, b } console.timeEnd("cache");
cache: 42ms
→ undefined

Screenshot: http://img.gf3.ca/04cf9935a84a44ea5227e0bac0f11b32.png

Please sign in to comment.