ColdFusion Identity Hash Code

I'm doing some CF R&D (basically I have a little free time, and I was thinking about playing with some things) – and I needed a hash code that would be specific to an object- more often than not a complex object like a Struct or Array.

After a bit of experimentation I found that most of the complex structures that are utilsed in CF (Vectors and Hashtables) return either 0 or 1 as a hashCode(), which is not all that useful.

I finally came along to java.lang.System.identityHashCode() which gives you a specific number that is unique to that object. It is important to note that if the object changes (i.e. you append a value to the array for example) the value of identityHashCode stays the same.

So if you were to compare two identiyHashCodes from different objects, if they were the same, you know them to be exactly the same object.

So of course, i came up with a little UDF for this:

EDIT :::: made a small mistake when I first wrote this UDF as I was walking out the door… fixing it now.

function identityHashCode(object)
var system = createObject("java", "java.lang.System");
return system.identityHashCode(arguments.object);

Nothing very exciting in the UDF, but an interesting piece of code never the less, if you are trying to muck around with some of the inner workings of Objects in CF.

Leave a Comment


  • dave ross | November 10, 2004

    I think you mean:
    function identityHashCode(object)
    var system = createObject("java", "java.lang.System");
    return system.identityHashCode(object);

    Cool nonetheless!

  • M. Schopman | November 10, 2004

    Anyone checked the performance hit of this function? It could be a good replacement for createUUID() which is extremily slow under CFMX6.1 (10ms per action)

  • Tim Blair | November 10, 2004

    Performance seems good — a quick test and I was coming up with times around 0.03ms per call.

    However, if it’s a good replacement for createuuid(), I’m not so sure — there’s no guarantee that the hash created will be unique; in fact I’d put money on it being based on the memory location of the object (hence the reason you’ll get the same hash code however many times you call the function on the same object).

    createUUID() is guaranteed to be unique and can therefore be used (for example) as a primary key in a DB. You may be able to use the hascode for something like a memory-resident object cache or similar (in fact I’ve done just this in the past) but I wouldn’t use it as a createUUID() replacement.


  • mark | November 11, 2004


    Good point… wasn’t looking properly!

    Thanks for the heads up Dave.

  • Pragnesh Rana | April 5, 2007

    This is really very nice post. I was stuck with a simillar problem and this post has helped me out.

    Thanks buddy

  • infinity0 | July 14, 2009

    This is WRONG. Two objects that have different hashcodes are different, BUT two objects that have the same hashcode are NOT NECESSARY identical, even for System.identityHashCode().

    if you don’t believe me, generate 2^16 random objects and test their hashcodes. This is the expected rate of pairwise collision given a 32-bit (int-sized) hash code