Skip to content

Question: is the built-in hash function sufficient?  #67

Description

@njlr

I was looking through the source and found that the hash function is quite simple:

import { stringHash } from '../common';

export function findHash (key: any): number {
  const hash: number = typeof key === 'number'
    ? key
    : typeof key === 'string'
      ? stringHash(key)
      : Math.abs(stringHash(JSON.stringify(key)));

  return hash;
}

I can see two problems that might occur with this.

First, JSON.stringify can be sensitive to property order, which might lead to unexpected behaviour:

> JSON.stringify({ x: 1, y: 2 })
'{"x":1,"y":2}'
> JSON.stringify({ y: 2, x: 1 })
'{"y":2,"x":1}'

Second, (and please correct me if I missed something!) it does not leave a possibility for customization. I think the set and map modules should be parameterized on a tuple of hash and equals. A default can be provided for convenience.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions