as-virtual
v0.2.0
Published
Virtual and reference data structures for AssemblyScript
Downloads
3,904
Maintainers
Readme
Virtual Type
The Virtual type is an efficient implementation for referencing segments of data without the need for constant memory allocations. This is particularly useful for tasks such as JSON parsing, where frequent slicing of strings can lead to high allocation rates. By using Virtual, you can reference memory segments directly, improving performance significantly. For example, this JSON implementation perviously parsed a Vec3 at 8,159,838m ops/s
and with as-variant, it hit 39,260,740m ops/s
.
Installation
npm install as-virtual
Usage
import { Virtual } from "as-virtual/assembly";
const virt = new Virtual<string>("foo bar", 3 << 1);
// Equiv. to "foo bar".slice(3)
// Note that we are referencing the memory directly and are using UTF-16
virt.equals("foo"); // true
virt.equals("baz"); // false
const virt2 = new Virtual<string>(" baz");
// Reinstantiate Virtual with new data
virt.reinst("bar", 6 << 1);
// Concat "bar" and " baz"
virt.merge(virt2);
console.log(virt.copyOut());