Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Bun.hash.xxHash32|64() #16397

Merged
merged 3 commits into from
Jan 15, 2025
Merged

Conversation

versecafe
Copy link
Contributor

@versecafe versecafe commented Jan 14, 2025

What does this PR do?

This adds the xxHash32 & 64 algorithms to Bun.hash and the bun polyfill using zigs std.hash

  • Documentation or TypeScript types
  • Code changes

How did you verify your code works?

Wrote automated tests

  • I checked the lifetime of memory allocated to verify it's (1) freed and (2) only freed when it should be
  • I included a test for the new code, or an existing test covers it
  • JSValue used outside outside of the stack is either wrapped in a JSC.Strong or is JSValueProtect'ed
  • I wrote TypeScript/JavaScript tests and they pass locally (bun-debug test test-file-name.test)
import { describe, it, expect } from "bun:test";

describe("xxHash functions", () => {
  it("Bun.hash.xxHash32()", () => {
    expect(Bun.hash.xxHash32("hello world")).toBe(0xcebb6622);
    expect(Bun.hash.xxHash32(new TextEncoder().encode("hello world"))).toBe(
      0xcebb6622,
    );
  });
  it("Bun.hash.xxHash64()", () => {
    expect(Bun.hash.xxHash64("hello world")).toBe(0x45ab6734b21e6968n);
    expect(Bun.hash.xxHash64(new TextEncoder().encode("hello world"))).toBe(
      0x45ab6734b21e6968n,
    );
  });
  it("Bun.hash.xxHash3()", () => {
    expect(Bun.hash.xxHash3("hello world")).toBe(0xd447b1ea40e6988bn);
    expect(Bun.hash.xxHash3(new TextEncoder().encode("hello world"))).toBe(
      0xd447b1ea40e6988bn,
    );
  });
});

@Gobd
Copy link
Contributor

Gobd commented Jan 14, 2025

Does Zig also have xxHash3 and if so could that also be added?

@versecafe
Copy link
Contributor Author

@Gobd added xxhash3

@Jarred-Sumner Jarred-Sumner merged commit 6cb0d49 into oven-sh:main Jan 15, 2025
59 of 63 checks passed
@Jarred-Sumner
Copy link
Collaborator

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants