What is ohash?
The ohash npm package is a utility library for handling objects and hashes in JavaScript. It provides a variety of functions to manipulate and work with objects, including creating hashes, deep cloning, and merging objects.
What are ohash's main functionalities?
Creating hashes from objects
This feature allows you to create a unique hash string from a JavaScript object. It is useful for identifying objects or storing them in a hash table.
const ohash = require('ohash');
const myObject = { name: 'John', age: 30 };
const hash = ohash.hash(myObject);
console.log(hash);
Deep cloning objects
This feature provides a method to create a deep clone of an object, ensuring that nested objects are also cloned rather than just copying references.
const ohash = require('ohash');
const original = { name: 'John', details: { age: 30, city: 'New York' } };
const cloned = ohash.clone(original);
console.log(cloned);
Merging objects
This feature allows you to merge two or more objects into a single object, combining their properties. If properties overlap, the last object's properties will take precedence.
const ohash = require('ohash');
const object1 = { name: 'John' };
const object2 = { age: 30 };
const mergedObject = ohash.merge(object1, object2);
console.log(mergedObject);
Other packages similar to ohash
lodash
Lodash is a comprehensive utility library that offers similar functionalities to ohash, such as deep cloning and merging objects. Lodash is more extensive and widely used in the industry, providing a broader range of functions and better performance optimizations.
deepmerge
Deepmerge is a package specifically designed for merging objects deeply. While ohash provides this functionality, deepmerge offers more advanced options for controlling the merge process, such as array concatenation and custom merge functions.
#️ ohash

Simple object hashing, serialization and comparison utils.
[!NOTE]
You are on active v2 development branch. Check v1 for old ohash v1 docs and release notes for migration.
Usage
Install ohash
:
npx nypm i ohash
Import:
import { hash, serialize, digest } from "ohash";
import { isEqual, diff } from "ohash/utils";
const { hash, serialize, digest } = await import("ohash");
const { isEqual, diff } = await import("ohash/utils");
Import from CDN
import { hash, serialize, digest } from "https://esm.sh/ohash";
import { isEqual, diff } from "https://esm.sh/ohash/utils";
const { hash, serialize, digest } = await import("https://esm.sh/ohash");
const { isEqual, diff } = await import("https://esm.sh/ohash/utils");
hash(input)
Hashes any JS value into a string.
The input is first serialized then it is hashed.
import { hash } from "ohash";
console.log(hash({ foo: "bar" }));
serialize(input)
Serializes any input value into a string for hashing.
[!IMPORTANT]
serialize
method uses best efforts to generate stable serialized values; however, it is not designed for security purposes. Keep in mind that there is always a chance of intentional collisions caused by user input.
import { serialize } from "ohash";
console.log(serialize({ foo: "bar" }));
digest(str)
Hashes a string using the SHA-256 algorithm and encodes it in Base64URL format.
import { digest } from "ohash";
console.log(digest("Hello World!"));
isEqual(obj1, obj2)
Compare two objects using ===
and then fallbacks to compare based on their serialized values.
import { isEqual } from "ohash/utils";
console.log(isEqual({ a: 1, b: 2 }, { b: 2, a: 1 }));
diff(obj1, obj2)
Compare two objects with nested serialization. Returns an array of changes.
The returned value is an array of diff entries with $key
, $hash
, $value
, and $props
. When logging, a string version of the changelog is displayed.
import { diff } from "ohash/utils";
const createObject = () => ({
foo: "bar",
nested: {
y: 123,
bar: {
baz: "123",
},
},
});
const obj1 = createObject();
const obj2 = createObject();
obj2.nested.x = 123;
delete obj2.nested.y;
obj2.nested.bar.baz = 123;
const diff = diff(obj1, obj2);
console.log(diff(obj1, obj2));
Contribute
- Clone this repository
- Enable Corepack using
corepack enable
- Install dependencies using
pnpm install
- Run interactive tests using
pnpm dev
License
Made with 💛 Published under MIT License.
Object serialization originally based on puleos/object-hash by Scott Puleo.
sha256 implementation originally based on brix/crypto-js.