Skip to content

Commit

Permalink
Use MurmurHash3 when native hashing function is not available
Browse files Browse the repository at this point in the history
Adds a pure JS implementation of a string hashing function. We do not
use it for security or obfuscation purposes, only to create compact
hashes. So we prioritize speed over collision avoidance. For example, we
use this to hash the component key path used by useFormState for
MPA-style submissions. See facebook#27397 for details.

In environments where built-in hashing functions are available, we
prefer those instead. Like Node's crypto module, or Bun.hash.
Unfortunately this does not include the web standard crypto API because
those methods are all async. For our purposes, we need it to be sync
because the cost of context switching is too high to be worth it.

The most popular hashing algorithm that meets these requirements in the
JS ecosystem is MurmurHash3, and almost all implementations I could find
used some version of the implementation by Gary Court. So that's the
one I've used.

In the future we should try to migrate these to native calls whenever
possible. It's especially unfortunate that the Edge build doesn't use
a native implementation, because that's the one used by newer frameworks
like Next.js.
  • Loading branch information
acdlite committed Sep 20, 2023
1 parent 2b3d582 commit 4803cbf
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,4 @@ export function closeWithError(destination: Destination, error: mixed): void {
destination.destroy(error);
}

export function createFastHash(input: string): string | number {
return input;
}
export {createFastHashJS as createFastHash} from 'react-server/src/createFastHashJS';
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,4 @@ export function closeWithError(destination: Destination, error: mixed): void {
destination.error = error;
}

export function createFastHash(input: string): string | number {
return input;
}
export {createFastHashJS as createFastHash} from 'react-server/src/createFastHashJS';
4 changes: 1 addition & 3 deletions packages/react-server/src/ReactServerStreamConfigBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,4 @@ export function closeWithError(destination: Destination, error: mixed): void {
}
}

export function createFastHash(input: string): string | number {
return input;
}
export {createFastHashJS as createFastHash} from 'react-server/src/createFastHashJS';
4 changes: 1 addition & 3 deletions packages/react-server/src/ReactServerStreamConfigEdge.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,4 @@ export function closeWithError(destination: Destination, error: mixed): void {
}
}

export function createFastHash(input: string): string | number {
return input;
}
export {createFastHashJS as createFastHash} from 'react-server/src/createFastHashJS';

0 comments on commit 4803cbf

Please sign in to comment.