Object id, hash, etc?

Bradley Meck bradley.meck at gmail.com
Thu Sep 10 18:05:45 UTC 2015


I would avoid using Object identities on primitives as you can do `===`
comparisons if IDs are serialized and have differing IDs for equivalent
objects.

On Thu, Sep 10, 2015 at 1:02 PM, Michael McGlothlin <
mike.mcglothlin at gmail.com> wrote:

> One issue I did notice with WeakMaps is that because it can't use
> primative values as keys that they'll throw errors unless otherwise
> handled. Checking if they are an instanceof Object and if not using a Map
> instead seems to work.
>
> I wouldn't think using primatives as WeakMap keys would be an issue.
>
>
>    const identities = new WeakMap();
>    const primatives = new Map();
>    let OBJECT_ID = 0;
>
>    function getObjectIdentity() {
>     if ( this instanceof Object ) {
>      if ( !identities.has( this ) ) {
>       identities.set( this, ++OBJECT_ID );
>      }
>      return identities.get( this );
>     }
>
>     if ( !primatives.has( this ) ) {
>      primatives.set( this, ++OBJECT_ID );
>     }
>     return primatives.get( this );
>    }
>
>
> On Wed, Sep 9, 2015 at 8:14 AM, Michael McGlothlin <
> mike.mcglothlin at gmail.com> wrote:
>
>> I think using WeakMaps is a good idea. Using a Symbol will keep from
>> cluttering the namespace with names others might want to use (id,label,etc)
>> and seems more consistent than some external means of accessing the id.
>> When I tried defining the value before I ran into issues with the prototype
>> chain which is why I was using get and a second 'hidden' property. Using
>> WeakMap seems like it'd fix the issue.
>>
>> Can't say I've used Object.assign much - will have to experiment with it.
>>
>> Thanks,
>> Michael McGlothlin
>> Sent from my iPhone
>>
>> On Sep 9, 2015, at 7:34 AM, Andrea Giammarchi <
>> andrea.giammarchi at gmail.com> wrote:
>>
>> Well, to start with, you don't hide symbols, these are all exposed
>> through `Object.getOwnPropertySymbols` or Reflect.ownKeys so for your use
>> case looks like using just a non enumerable property would do as well.
>> And btw, even if Symbols don't show up in for/in and Object.keys, these
>> are by default also enumerable so Object.assign will copy them around
>> (reason I've used defineProperty with the value instead of just setting the
>> symbol, so it's non enumerable by default).
>>
>> As summary: do you want to be sure that object has a unique "lable" or
>> "id" associated with it? Use WeakMaps, otherwise be prepared to possible
>> clones around if some library uses Object.assign thinking that's a good way
>> to copy properties around.
>>
>> I still suggest the Labeler approach that Mark previously indicated.
>>
>> Regards
>>
>>
>>
>>
>> On Wed, Sep 9, 2015 at 1:15 PM, Michael McGlothlin <
>> mike.mcglothlin at gmail.com> wrote:
>>
>>> Using frozen objects and such seems a lot more complex to me than just
>>> using a property. Using a symbol to hide the property from cluttering seems
>>> to go along with how other things are being done. And it's my understanding
>>> that other than hiding it that using a Symbol for a name is nothing special
>>> - does it have some negative factor that should make using it less
>>> attractive?
>>>
>>> 📱 Michael McGlothlin
>>>
>>> On Sep 9, 2015, at 5:36 AM, Andrea Giammarchi <
>>> andrea.giammarchi at gmail.com> wrote:
>>>
>>> That's similar to the Labeler proposed by Mark, except it needs to
>>> search for the thing twice (wm.has + wm.get instead of just wm.get which
>>> would be  just fine as check if you count from 1 instead of 0 ;-) )
>>>
>>> Although I need to understand if you have a wm why wouldn't you just use
>>> that instead of a Symbol ... so the Labeler seems again a more appropriate
>>> pattern for this problem.
>>>
>>> Just my thoughts
>>>
>>> On Wed, Sep 9, 2015 at 10:51 AM, Michał Wadas <michalwadas at gmail.com>
>>> wrote:
>>>
>>>> Following solution will work:
>>>>
>>>> (function(){
>>>> const wm = new WeakMap();
>>>> Symbol.identity = Symbol( 'Symbol.identity' );
>>>> let i = 0;
>>>> Object.defineProperty(
>>>>   Object.prototype,
>>>>   Symbol.identity,
>>>>   {
>>>>     get: function () {
>>>>       if (wm.has(this)) return wm.get(this);
>>>>       wm.set(this, Symbol(i++));
>>>>       return wm.get(this);
>>>>     }
>>>>   }
>>>> );
>>>> })()
>>>>
>>>>
>>>> 2015-09-08 23:29 GMT+02:00 Andrea Giammarchi <
>>>> andrea.giammarchi at gmail.com>:
>>>>
>>>>> as side note, that's just a lazy assignment that doesn't need two
>>>>> symbols and a constant get invoke ...
>>>>>
>>>>> ```js
>>>>> Symbol.identity = Symbol( 'Symbol.identity' );
>>>>> var OBJECT_ID = 0;
>>>>> Object.defineProperty(
>>>>>   Object.prototype,
>>>>>   Symbol.identity,
>>>>>   {
>>>>>     get: function () {
>>>>>       // first time this is invoked ... and no more ...
>>>>>       Object.defineProperty(this, Symbol.identity, {value:
>>>>> ++OBJECT_ID});
>>>>>       // from now own, direct property access \m/
>>>>>       return this[Symbol.identity];
>>>>>     }
>>>>>   }
>>>>> );
>>>>> ```
>>>>>
>>>>> Regards
>>>>>
>>>>> On Tue, Sep 8, 2015 at 9:57 PM, Michael McGlothlin <
>>>>> mike.mcglothlin at gmail.com> wrote:
>>>>>
>>>>>> I try to keep it pretty simple. It's not fancy but the times you want
>>>>>> fast and dirty information like this are the same times you don't want to
>>>>>> have to define it manually.
>>>>>>
>>>>>>  Symbol.identity = Symbol( 'Symbol.identity' );
>>>>>>  const identity = Symbol( 'identity' );
>>>>>>  var OBJECT_ID = 0;
>>>>>>  Object.defineProperty( Object.prototype, Symbol.identity, {
>>>>>>   get: () => {
>>>>>>    if ( !Object.hasOwnProperty.call( this, identity ) ) {
>>>>>>     this[ identity ] = ++OBJECT_ID;
>>>>>>    }
>>>>>>    return this[ identity ];
>>>>>>   }
>>>>>>  } );
>>>>>>
>>>>>> On Tue, Sep 8, 2015 at 1:44 PM, Mark S. Miller <erights at google.com>
>>>>>> wrote:
>>>>>>
>>>>>>> See Labeler at
>>>>>>>
>>>>>>> http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Tue, Sep 8, 2015 at 11:16 AM, joe <joeedh at gmail.com> wrote:
>>>>>>>
>>>>>>>> Didn't send to list, something is wrong with my reply all. Sorry
>>>>>>>> about that. Stupid mobile gmail.
>>>>>>>> ---------- Forwarded message ----------
>>>>>>>> From: "joe" <joeedh at gmail.com>
>>>>>>>> Date: Sep 8, 2015 11:15 AM
>>>>>>>> Subject: Re: Object id, hash, etc?
>>>>>>>> To: "Garrett Smith" <dhtmlkitchen at gmail.com>
>>>>>>>> Cc:
>>>>>>>>
>>>>>>>> I agree with this request. This is the logical complement to
>>>>>>>> valueof, I think. And yes, for most ID use cases this isn't a good fit, but
>>>>>>>> we're not talking about the general case, just the cases where a python
>>>>>>>> style id() function *is* appropriate.
>>>>>>>>
>>>>>>>> Joe
>>>>>>>> On Sep 8, 2015 9:08 AM, "Garrett Smith" <dhtmlkitchen at gmail.com>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> On 9/8/15, Michael McGlothlin <mike.mcglothlin at gmail.com> wrote:
>>>>>>>>> > Is there a reason not to provide an object id and hash value as
>>>>>>>>> other
>>>>>>>>> > languages often do? I find myself defining an identity symbol on
>>>>>>>>> objects
>>>>>>>>> > with a value from a simple global counter. It makes it easier to
>>>>>>>>> debug if I
>>>>>>>>> > can just look at a log and see what objects and functions were
>>>>>>>>> active.
>>>>>>>>> >
>>>>>>>>> > Likewise a hash value would simplify a lot of comparisons and
>>>>>>>>> make it easier
>>>>>>>>> > to debug.
>>>>>>>>> >
>>>>>>>>> > I especially find the ID useful when working with bound
>>>>>>>>> functions as they
>>>>>>>>> > can be difficult to tell apart. I like to change toString() to
>>>>>>>>> provide the
>>>>>>>>> > ID followed by the code (flattened to one line).
>>>>>>>>> >
>>>>>>>>> >
>>>>>>>>>
>>>>>>>>> NFE's are safe to use where IE8 support isn't needed*.
>>>>>>>>> (function aa(){}).name; // "aa"
>>>>>>>>>
>>>>>>>>> As for using Object IDs and Object Pooling, lexically-scoped values
>>>>>>>>> have benefits over global ID generators (as your counter). The
>>>>>>>>> values
>>>>>>>>> can be passed in as a parameter to the Factory (useful with Private
>>>>>>>>> Proxy). There other benefits to this pattern regarding memory
>>>>>>>>> management and application design. Contact me if you'd like to
>>>>>>>>> learn
>>>>>>>>> more.
>>>>>>>>>
>>>>>>>>> * https://kangax.github.io/nfe/
>>>>>>>>> --
>>>>>>>>> Garrett
>>>>>>>>> @xkit
>>>>>>>>> ChordCycles.wordpress.com
>>>>>>>>> garretts.github.io
>>>>>>>>> personx.tumblr.com
>>>>>>>>> _______________________________________________
>>>>>>>>> es-discuss mailing list
>>>>>>>>> es-discuss at mozilla.org
>>>>>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>>>>>
>>>>>>>>
>>>>>>>> _______________________________________________
>>>>>>>> es-discuss mailing list
>>>>>>>> es-discuss at mozilla.org
>>>>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>>     Cheers,
>>>>>>>     --MarkM
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> es-discuss mailing list
>>>>>>> es-discuss at mozilla.org
>>>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>>>
>>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> es-discuss mailing list
>>>>>> es-discuss at mozilla.org
>>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>>
>>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> es-discuss mailing list
>>>>> es-discuss at mozilla.org
>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>
>>>>>
>>>>
>>>
>>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150910/89645e8e/attachment-0001.html>


More information about the es-discuss mailing list