In response of optimization of equiv (QUnit same assertion service  
function)
>
> even shorter
>
> 52                  for (var i in a) {
> 53                      if (!a.hasOwnProperty(i) || !equiv(a[i],
> b[i])) {
> 54                        return false;
> 55                      }
> 56                  }


Markus the "optimization" you suggested leads to some problems.
By the way my test suites failed trying it.

My explanation:

Lets take o (an object) and i (a property of o) and b (and object to  
be compare with o):
It is not because  o  doesn't have the own property on a property  i   
that we should conclude that  o  is not equal to  b.
It makes no sense and this is what your code do.

So using your code will behave correctly with:
----------------------------------------------------------------------------
function A() {
}
var a1 = new A();
var a2 = new A();
equiv(a1,a2); // => true (ok there is no problem here)
----------------------------------------------------------------------------

But will behave badly with this:
----------------------------------------------------------------------------
function A() {
}
A.prototype.foo = "anything"; // because of that line
var a1 = new A();
var a2 = new A();
equiv(a1,a2); // => false (oops! it should be true!)
----------------------------------------------------------------------------


But in fact you are right that there should be a test check not to  
loop when differences have been found.
I've optimized it, but figured out that my test suites were taking  
140ms instead of 120ms with that optimization:
        
        // ..
         // replace line #48 to #66 of http://philrathe.com/articles/equiv#code
         function sameObj(a,b) {
             for (var i in a) {
                 if (a.hasOwnProperty(i)) {
                     if (!equiv(a[i], b[i])) {
                         return false; // break as soon a difference  
is detected
                     }
                 }
             }
             return true;
         }

         if (typeof a === "object") {
             // Verify properties equivalence in both ways:

             // Everything in a should be in b and equivalent and ...
             return  sameObj(a, b) &&
             // ... everything in b should be in a and equivalent
                     sameObj(b, a); // won't be called if a difference  
have been already noticed
         }
        // ...

This may due to the fact that in general my actual and expected are  
already equals so the optimization is overkill.
I have to investigate this.



On 21-Oct-08, at 1:35 PM, markus.staab wrote:

>
> even shorter
>
> 52                  for (var i in a) {
> 53                      if (!a.hasOwnProperty(i) || !equiv(a[i],
> b[i])) {
> 54                        return false;
> 55                      }
> 56                  }
>
> On 21 Okt., 19:07, "markus.staab" <[EMAIL PROTECTED]> wrote:
>> taking a second look at the for (x in y) loops, we could also do some
>> further optimization:
>>
>> 52                  for (var i in a) {
>> 53                      if (a.hasOwnProperty(i)) {
>> 54                          if(!equiv(a[i], b[i]))
>>                                return false;
>> 55                      } else {
>>                             return false;
>>                          }
>> 56                  }
>>
>> markus.staab schrieb:
>>
>>> in reply to the article athttp://philrathe.com/articles/equiv:
>>
>>> in the equiv method there is several times a loop like
>>
>>> 34             for (var i = 0; i < len; i++) {
>>> 35                 eq = eq && equiv(a[i], b[i]);
>>> 36             }
>>> 37             return eq;
>>
>>> this could be optimized, because if one of the elements is not  
>>> equal,
>>> you found, that the origin elements aren't equal..
>>
>>> so better use
>>
>>> 34             for (var i = 0; i < len && eq; i++) {
>>> 35                 eq = eq && equiv(a[i], b[i]);
>>> 36             }
>>> 37             return eq;
>>
>>> see the additional abort condition in the for loop...
>>> This little "trick" could be applied in several places of the
>>> function, e.g.
>>
>>> 52             for (var i in a) {
>>> 53                 if (a.hasOwnProperty(i)) {
>>> 54                     eq = eq && equiv(a[i], b[i]);
>>> 55                 }
>>> 56             }
>>
>>> 59             for (var i in b) {
>>> 60                 if (b.hasOwnProperty(i)) {
>>> 61                     eq = eq && equiv(b[i], a[i]);
>>> 62                 }
>>> 63             }
>>
>>> in the for(x in y) there should be a break, since there is no abort
>>> condition.
>>
>>> greets, markus
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to