DEV Community

Soatra
Soatra

Posted on

๐—ฉ๐—ฟ๐—ฒ๐—ณ โ€” JavaScript library for ๐—ฟ๐—ฒ๐—ฎ๐—ฐ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—บ๐˜‚๐˜๐—ฎ๐˜๐—ถ๐—ผ๐—ป and ๐—ฐ๐—ต๐—ฎ๐—ป๐—ด๐—ฒ ๐˜๐—ฟ๐—ฎ๐—ฐ๐—ธ๐—ถ๐—ป๐—ด.

Vref is Tiny Library for Safe Mutation and Change Tracking in JavaScript

JavaScript lets us freely mutate objects:

user.name = 'John';
user.age = 25;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ 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 });
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
const { value: settings } = ref(new Map(), (e) => console.log(e));
settings.set('theme', 'dark');  // triggers
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฆ 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)