Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Use exposed equals inside watch and equals #5913

Closed
wants to merge 3 commits into from

Conversation

lee-elenbaas
Copy link

This will allow using the proxy pattern for providing better equals implementation for specific cases

very good when you use an existing widget that performs watch on some property that you can provide a faster way to equal check it but you want to do it without changing the widget code

@mary-poppins
Copy link

I'm sorry, but I wasn't able to verify your Contributor License Agreement (CLA) signature. CLA signature is required for any code contributions to AngularJS.

Please sign our CLA and ensure that the CLA signature email address and the email address in this PR's commits match.

If you signed the CLA as a corporation, please let us know the company's name.

Thanks a bunch!

PS: If you signed the CLA in the past then most likely the email addresses don't match. Please sign the CLA again or update the email address in the commit of this PR.
PS2: If you are a Googler, please sign the CLA as well to simplify the CLA verification process.

@@ -820,7 +820,7 @@ function equals(o1, o2) {
if (!isArray(o2)) return false;
if ((length = o1.length) == o2.length) {
for(key=0; key<length; key++) {
if (!equals(o1[key], o2[key])) return false;
if (!angular.equals(o1[key], o2[key])) return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand? what is the motivation for this? so that you can customize the equals function?

Copy link
Author

Choose a reason for hiding this comment

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

exactly! when i know that in my SPA there are a lot of comparisons of a specific type, i can create a better comparison for that type (that depends only on the minimum number of properties that object needs. and then use the proxy pattern to replace the equals so that it will perform better for my SPA

@IgorMinar
Copy link
Contributor

this doesn't look right.

we rely on equals implementation heavily at many places throughout the framework, we can't let you monkey patch it and hope for the best.

it would be better if you described your use-cases in a bug/feature issue and then we can decide how to deal with it.

@IgorMinar IgorMinar closed this Jan 22, 2014
@lee-elenbaas
Copy link
Author

Of course angular depends heavily on equals - this is exactly why it is important that i will be able to enhance equals to fit better for my own SPA

Assume that there are a lot of watchs on instances of MyClass
One way to improve performance is by minimizing the number of watches. I can do this easily enough in my own code, but if i use some other library, my ability to limit the number of watch that library adds is limited. It is enough that that library will use ng-repeat on a collection containing MyClass instances that i can benefit from making equals perform better for MyClass

the proxy pattern for doing that will be something like this:

angular.module('betterMyClassEquals','MyClassModule').run(function(){
  var angularEquals = angular.equals;

  angular.equals = function(o1, o2) {
    if (o1 === o2) return true;
    if ((o1 instanceof MyClass) || (o2 instanceof MyClass)) {
      if ((o1 instanceof MyClass) && (o2 instanceof MyClass)) {
        return true; // here you will place the custome code for MyClass comparison
      }
      return false;
    }
    return angularEquals(o1, o2);
  };
});

@aleclofabbro
Copy link

👍
I would make it even more simple:
something like this :

function equals(o1, o2) {
    if (o1 === o2) return true;
    if (o1 === null || o2 === null) return false;
    if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN
    var t1 = typeof o1, t2 = typeof o2, length, key, keySet;
    if (t1 == t2) {
      if (t1 == 'object') {
        if (o1.$equals) return o1.$equals(o2);           // if one or both objects implements
        else if (o2.$equals) return o2.$equals(o1);   // the "reserved" $equals, then use it !
...
...

@lee-elenbaas
Copy link
Author

Setting a comparable interface works too. With less danger for framework
corruption. But it feels less javascript for me. A better approsch is an
api for adding comparators with priority. A cmparator will have 3 states
for each pair of objects: equal, not equal and not mine (can be true, false
and undefined) this structure will allow for breaking down the current
equals to smaller functions and for adding custom comparators specific for
the app. I will see if i can write it up
בתאריך 30 בינו 2014 16:16, "aleclofabbro" [email protected] כתב:

[image: 👍]
I would make it even more simple:
something like this :

function equals(o1, o2) {
if (o1 === o2) return true;
if (o1 === null || o2 === null) return false;
if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN
var t1 = typeof o1, t2 = typeof o2, length, key, keySet;
if (t1 == t2) {
if (t1 == 'object') {
if (o1.$equals) return o1.$equals(o2); // if one or both objects implements
else if (o2.$equals) return o2.$equals(o1); // the "reserved" $equals, then use it !
...
...


Reply to this email directly or view it on GitHubhttps://github.com//pull/5913#issuecomment-33690758
.

@lee-elenbaas lee-elenbaas deleted the master branch February 2, 2014 10:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants