From 42c1e251f5f91fddd27cdb0bb8752f0d46cef0a5 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sun, 15 Sep 2024 15:01:23 +0200 Subject: [PATCH] update --- README.md | 11 +++++++---- benchmark/hash.mjs | 8 ++++---- src/hash.ts | 2 +- src/index.ts | 2 +- test/index.test.ts | 6 +++--- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4412ef5..091f67e 100644 --- a/README.md +++ b/README.md @@ -27,23 +27,26 @@ Import: ```js // ESM -import { hash, objectHash, murmurHash, sha256 } from "ohash"; +import { hash, hashAsync, objectHash, murmurHash, sha256 } from "ohash"; // CommonJS -const { hash, objectHash, murmurHash, sha256 } = require("ohash"); +const { hash, hashAsync, objectHash, murmurHash, sha256 } = require("ohash"); ``` -### `hash(object, options?)` +### `hash(object, options?)` / `hashAsync(object, options?)` Converts object value into a string hash using `objectHash` and then applies `sha256` with Base64 encoding (trimmed by length of 10). +`hashAsync` is slightly faster as will leverage [`SubtleCrypto.digest`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest) when available. + Usage: ```js -import { hash } from "ohash"; +import { hash, hashAsync } from "ohash"; // "dZbtA7f0lK" console.log(hash({ foo: "bar" })); +console.log(await hashAsync({ foo: "bar" })); ``` ### `objectHash(object, options?)` diff --git a/benchmark/hash.mjs b/benchmark/hash.mjs index 445c1cb..0c32c44 100644 --- a/benchmark/hash.mjs +++ b/benchmark/hash.mjs @@ -1,5 +1,5 @@ import Benchmark from "benchmark"; -import { hash, asyncHash } from "ohash"; +import { hash, hashAsync } from "ohash"; import largeJson from "./fixture/large.mjs"; import { generateItems } from "./_utils.mjs"; @@ -19,11 +19,11 @@ for (const [name, data] of Object.entries(dataSets)) { hash(data); }); suite.add( - `asyncHash(${name})`, + `hashAsync(${name})`, (ctx) => { - asyncHash(data).then(() => ctx.resolve()); + hashAsync(data).then(() => ctx.resolve()); }, - { defer: true } + { defer: true }, ); } diff --git a/src/hash.ts b/src/hash.ts index 81edb5d..745d059 100644 --- a/src/hash.ts +++ b/src/hash.ts @@ -14,7 +14,7 @@ export function hash(object: any, options: HashOptions = {}): string { return sha256base64(hashed).slice(0, 10); } -export async function asyncHash( +export async function hashAsync( object: any, options: HashOptions = {}, ): Promise { diff --git a/src/index.ts b/src/index.ts index aa1c47b..d093199 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ export { objectHash } from "./object-hash"; -export { hash, asyncHash } from "./hash"; +export { hash, hashAsync } from "./hash"; export { murmurHash } from "./crypto/murmur"; export { sha256, sha256base64 } from "./crypto/sha256"; export { isEqual } from "./utils"; diff --git a/test/index.test.ts b/test/index.test.ts index de1e42f..2696f7d 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -6,7 +6,7 @@ import { sha256, isEqual, diff, - asyncHash, + hashAsync, } from "../src"; import { sha256base64 } from "../src/crypto/sha256"; @@ -100,8 +100,8 @@ it("hash", () => { expect(hash({ foo: "bar" })).toMatchInlineSnapshot('"dZbtA7f0lK"'); }); -it("asyncHash", async () => { - expect(await asyncHash({ foo: "bar" })).toMatchInlineSnapshot('"dZbtA7f0lK"'); +it("hashAsync", async () => { + expect(await hashAsync({ foo: "bar" })).toMatchInlineSnapshot('"dZbtA7f0lK"'); }); describe("isEqual", () => {