DEV Community

Dr Codewell
Dr Codewell

Posted on

Building a Rust-Like Runtime for JavaScript in React

*JavaScript is flexible. Sometimes *too flexible.
**
A string becomes a number.
undefined appears from nowhere.
Async code silently fails.
One mutated object breaks an entire component tree.

After fighting those problems for years, I started experimenting with something different:


A runtime layer for JavaScript that brings stricter memory handling, typed structures, controlled loops, and safer async execution — while still working inside React.

I call the idea StrictJS Runtime.


Why I Started Building It

Modern frontend apps are becoming extremely complex:

  • Realtime dashboards
  • ML in the browser
  • Heavy state synchronization
  • WebAssembly integrations
  • Streaming APIs
  • Massive React trees

But JavaScript still allows things like:

const user = {};
user.profile.name.first.last = "Ken";
Enter fullscreen mode Exit fullscreen mode

…and we only discover the mistake at runtime.

I wanted something closer to systems programming ideas:

  • predictable structures
  • explicit memory ownership
  • strict schemas
  • controlled execution
  • safer async behavior

Without abandoning JavaScript completely.


The Core Idea

Instead of replacing JavaScript, the runtime wraps dangerous behavior.

Example:

import strictInit from "strictjs-runtime";

const {
  StrictObject,
  StrictString,
  StrictNumber,
  Schema
} = await strictInit({});
Enter fullscreen mode Exit fullscreen mode

Now objects become schema-driven:

const UserSchema = new Schema({
  username: StrictString,
  age: StrictNumber
});

const user = new StrictObject(UserSchema, {
  username: "alex",
  age: 22
});
Enter fullscreen mode Exit fullscreen mode

Invalid data throws immediately:

user.age = "twenty two";
// Error
Enter fullscreen mode Exit fullscreen mode

Safer Async Functions

One thing I hate in frontend apps:

fetch("/api")
  .then(r => r.json())
  .then(data => ...)
Enter fullscreen mode Exit fullscreen mode

Errors disappear everywhere.

So I experimented with a StrictFunction wrapper:

const fetchUser = new StrictFunction(async () => {
  const res = await fetch("/api/user");
  return await res.json();
});
Enter fullscreen mode Exit fullscreen mode

The runtime can then:

  • track execution
  • monitor memory usage
  • enforce return structures
  • detect invalid async flows

Controlled Loops

Another experiment:

new StrictForLoop({
  start: 0,
  end: 1000,
  callback(i) {
    console.log(i);
  }
});
Enter fullscreen mode Exit fullscreen mode

Why?

Because large uncontrolled loops can freeze React rendering.

A runtime-managed loop can eventually support:

  • chunked execution
  • cancellation
  • cooperative scheduling
  • React-aware yielding

React Integration

React is where this becomes interesting.

Imagine state that cannot accidentally mutate:

const [user, setUser] = useState(
  new StrictObject(UserSchema, {
    username: "ken",
    age: 20
  })
);
Enter fullscreen mode Exit fullscreen mode

Or runtime-validated hooks:

const useStrictState = (schema, initial) => {
  const [state, setState] = useState(
    new StrictObject(schema, initial)
  );

  return [state, setState];
};
Enter fullscreen mode Exit fullscreen mode

Now your React state becomes structurally protected at runtime.


WebAssembly + Rust Direction

The long-term vision is pushing critical parts into Rust/WASM:

  • memory management
  • validation engine
  • async scheduler
  • reactive graph execution

JavaScript becomes the interface layer.

Rust becomes the execution core.

This hybrid model could make frontend apps significantly more predictable under heavy workloads.


Is It Faster?

Not always.

Strict systems add overhead.

But the goal is not just raw speed.

The goal is:

  • reliability
  • predictability
  • developer control
  • safer large-scale frontend systems

In some workloads — especially structured data processing — the tradeoff becomes worth it.


Example: Strict Fetch

const { strict_fetch } = await strictInit({});

const data = await strict_fetch("/api/users")
  .json();
Enter fullscreen mode Exit fullscreen mode

Potential future features:

  • typed responses
  • response validation
  • automatic retries
  • streaming control
  • memory tracking

Top comments (0)