Vref is Tiny Library for Safe Mutation and Change Tracking in JavaScript
JavaScript lets us freely mutate objects:
user.name = 'John';
user.age = 25;
But this doesn't trigger any change detection โ unless we use a large framework like React, Vue, MobX, or Immer.
Most existing solutions rely on:
โ Immutable updates (React setState)
โ Deep cloning (performance cost)
โ Heavy reactivity systems (Vue, MobX)
โ Framework-specific abstractions
โ Large bundles just to track simple changes
Sometimes, we just want something:
โ Tiny
โ Framework-agnostic
โ Native mutation
โ Safe and reactive
Thatโs why I built VRef.
๐ What is VRef?
VRef is a tiny JavaScript utility that lets you:
โ
Mutate values directly (no immutability, no cloning)
๐ช Get notified when changes happen
โก Works with objects, arrays, Maps, Sets
๐ Safe with Garbage Collection
๐ Works in both browser & Node.js
npm install vref
๐ Quick Example
import ref from 'vref';
// Create a reactive object with a change tracker
const { value: user } = ref(
// User's initial value
{ name: 'John', age: 25 },
// Change event handler
(event) => {
console.log('Changed:', event);
}
);
user.name = 'Doe'; // triggers change event
user.age = 26; // triggers change event
๐ Key point: You mutate normally, but VRef detects and reports changes.
๐ Why VRef Exists
Most libraries force you to use:
- Immutable patterns
- Cloning and diffing
- Framework-specific APIs
But sometimes, you just want to write this:
cart.items.push({ id: 1, qty: 2 });
And still know when and what changed โ without deep cloning, proxies everywhere, or big abstractions.
๐ How It Works (In Short)
- It wraps your object/array using
Proxy. - Intercepts mutations (
set,delete,get). - Fires your callback when something changes.
- Does not block normal mutation.
- Fully GC-safe โ no memory leaks.
๐งช Works with Arrays, Maps, Sets
const { value: todos } = ref([], (e) => console.log(e));
todos.push('Learn VRef'); // triggers
todos[0] = 'Practice VRef'; // triggers
const { value: settings } = ref(new Map(), (e) => console.log(e));
settings.set('theme', 'dark'); // triggers
๐ฆ When to Use VRef?
| Use case | Is VRef good? |
|---|---|
| Logging object mutations | โ Yes |
| Undo/redo or time-travel state | โ Yes |
| Form input tracking | โ Yes |
| Framework-level state management | โ ๏ธ Maybe |
| Full UI reactivity (React/Vue) | โ ๏ธ Maybe |
โ๏ธ Comparison
| Feature | VRef | MobX | Immer | Vue |
|---|---|---|---|---|
| Tiny | โ๏ธ | โ | โ | โ |
| Direct mutation | โ๏ธ | โ๏ธ | โ๏ธ | โ |
| Change tracking | โ๏ธ | โ๏ธ | โ ๏ธ | โ๏ธ |
| Framework-free | โ๏ธ | โ | โ๏ธ | โ |
๐ฑ Final Thoughts
VRef is not meant to replace state management libraries or frameworks.
Itโs a primitive, lightweight utility for simple, safe, reactive mutation and change tracking โ nothing more, nothing less.
๐ฏ Perfect when you want:
โI just want to know when something changes โ without rewriting my whole app.โ
๐ฆ Try it here:
npm: https://www.npmjs.com/package/vref
GitHub: https://github.com/JohnSoatra/vref
๐ฌ What do you think?
Would you find this useful in reactive state, logging, debugging, undo/redo, or form tracking?
I'd love feedback and suggestions!
Top comments (0)