Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

feat: sync back-references #56

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 56 additions & 16 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
// taken from https://github.com/Rich-Harris/superjson-and-devalue

import ARSON from "arson";
import { parse, stringify, uneval } from "devalue";
import * as devalue from "devalue";
import c from "kleur";
import * as superjson from "superjson";
import { createTson, tsonDate, tsonRegExp, tsonSet } from "tupleson";

const time_formatter = new Intl.NumberFormat("en-US", {
style: "unit",
unit: "millisecond",
});
const size_formatter = new Intl.NumberFormat("en-US", {
style: "unit",
unit: "byte",
});
const number_formatter = new Intl.NumberFormat("en-US");

const obj = {
array: [{ foo: 1 }, { bar: 2 }, { baz: 3 }],
date: new Date(),
Expand All @@ -14,37 +24,49 @@ const obj = {
set: new Set([1, 2, 3]),
xss: '</script><script>alert("XSS")</script>',
};

// circular references are not supported by tupleson
// obj.self = obj;
obj.self = obj;

const tson = createTson({
types: [tsonDate, tsonRegExp, tsonSet],
});

const superjson_serialized = superjson.stringify(obj);
const devalue_unevaled = uneval(obj);
const devalue_stringified = stringify(obj);
const devalue_unevaled = devalue.uneval(obj);
const devalue_stringified = devalue.stringify(obj);
const arson_stringified = ARSON.stringify(obj);
const tson_serialized = tson.stringify(obj);

console.log("-- SERIALIZED SIZE --\n");

console.log(
`superjson output: ${c.bold().cyan(superjson_serialized.length)} bytes`,
`superjson output: ${c
.bold()
.cyan(size_formatter.format(superjson_serialized.length))}`,
);

console.log(`tson output: ${c.bold().cyan(tson_serialized.length)} bytes`);
console.log(
`tson output: ${c
.bold()
.cyan(size_formatter.format(tson_serialized.length))}`,
);
// console.log(superjson_serialized);
console.log(
`devalue.uneval output: ${c.bold().cyan(devalue_unevaled.length)} bytes`,
`devalue.uneval output: ${c
.bold()
.cyan(size_formatter.format(devalue_unevaled.length))}`,
);
// console.log(devalue_unevaled);
console.log(
`devalue.stringify output: ${c
.bold()
.cyan(devalue_stringified.length)} bytes`,
.cyan(size_formatter.format(devalue_stringified.length))}`,
);
// console.log(devalue_stringified);
console.log(`arson output: ${c.bold().cyan(arson_stringified.length)} bytes`);
console.log(
`arson output: ${c
.bold()
.cyan(size_formatter.format(arson_stringified.length))}`,
);
// console.log(arson_stringified);

// const superjson_deserialized = superjson.parse(superjson_serialized);
Expand All @@ -53,31 +75,49 @@ console.log(`arson output: ${c.bold().cyan(arson_stringified.length)} bytes`);
const iterations = 1e6;

function test(fn, label = fn.toString()) {
const start = Date.now();
console.log();
console.log(c.bold(label));
global.gc(); // force garbage collection before each test
let i = iterations;
const before_snap = process.memoryUsage();
const start = Date.now();
while (i--) {
fn();
}

const delta = Date.now() - start;
const after_snap = process.memoryUsage();
console.log(
`${iterations} iterations in ${c.bold().cyan(Date.now() - start)}ms`,
`${number_formatter.format(iterations)} iterations in ${c
.bold()
.cyan(time_formatter.format(delta))}`,
);
// log memory usage delta
for (const key in after_snap) {
const before = before_snap[key];
const after = after_snap[key];
const diff = after - before;
const color = diff < 0 ? c.green : c.red;
console.log(` ${key}: ${color(size_formatter.format(diff))}`);
}
}

console.log("\n-- SERIALIZATION DURATION --");

// serialization
test(() => superjson.stringify(obj));
test(() => tson.stringify(obj));
test(() => uneval(obj));
test(() => stringify(obj));
test(() => devalue.uneval(obj));
test(() => devalue.stringify(obj));
test(() => ARSON.stringify(obj));

console.log("\n-- DESERIALIZATION DURATION --");

// deserialization
test(() => superjson.parse(superjson_serialized));
test(() => tson.parse(tson_serialized));
test(() => eval(`(${devalue_unevaled})`));
test(() => ARSON.parse(arson_stringified));
test(() => parse(devalue_stringified));
test(() => devalue.parse(devalue_stringified));

console.log();
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "module",
"scripts": {
"postinstall": "cd ../ && pnpm run build",
"start": "node index.js"
"start": "node --expose-gc --max-old-space-size=8192 index.js"
},
"dependencies": {
"arson": "^0.2.6",
Expand Down
Loading