bun icon indicating copy to clipboard operation
bun copied to clipboard

Add native object hashing

Open 7f8ddd opened this issue 2 years ago • 1 comments

What is the problem this feature would solve?

It would allow you to hash an object fast. object-hash or stringifying an object are not ideal. It's really ugly to stringify an object just to hash it, and ideally no allocations would be required (aside from the hash).

What is the feature you are proposing to solve the problem?

Implement something similar to what Java has, where you can do this:

class Main {
  public static void main(String[] args) {

    // hashCode() with Object
    Object obj1 = new Object();
    System.out.println(obj1.hashCode());  // 1785210046

    Object obj2 = new Object();
    System.out.println(obj2.hashCode());  // 1552787810

    Object obj3 = new Object();
    System.out.println(obj3.hashCode());  // 1361960727
  }
}

What alternatives have you considered?

object-hash (likely very slow) JSON.stringify (hacky)

7f8ddd avatar Apr 03 '24 16:04 7f8ddd

Thanks for the suggestion. If we were to do this, it would probably have to be Bun.hashCode(o) since we do not want to add a non-standard extension to the Object prototype.

Electroid avatar Apr 09 '24 00:04 Electroid