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

chore: add benchmark folder #51

Merged
merged 12 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
80 changes: 80 additions & 0 deletions .benchmark/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import ARSON from "arson";

Check failure on line 1 in .benchmark/index.js

View workflow job for this annotation

GitHub Actions / lint

"arson" is not found
import { parse, stringify, uneval } from "devalue";

Check failure on line 2 in .benchmark/index.js

View workflow job for this annotation

GitHub Actions / lint

"devalue" is not found
import c from "kleur";

Check failure on line 3 in .benchmark/index.js

View workflow job for this annotation

GitHub Actions / lint

"kleur" is not found
import * as superjson from "superjson";

Check failure on line 4 in .benchmark/index.js

View workflow job for this annotation

GitHub Actions / lint

"superjson" is not found
import { createTson, tsonDate, tsonRegExp } from "tupleson";

Check failure on line 5 in .benchmark/index.js

View workflow job for this annotation

GitHub Actions / lint

"tupleson" is not found

const obj = {
array: [{ foo: 1 }, { bar: 2 }, { baz: 3 }],
date: new Date(),
number: 42,
regex: /the quick brown fox/,
xss: '</script><script>alert("XSS")</script>',
};

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

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

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

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

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

// const superjson_deserialized = superjson.parse(superjson_serialized);
// const devalue_deserialized = eval(`(${devalue_unevaled})`);

const iterations = 1e6;

function test(fn, label = fn.toString()) {
const start = Date.now();
console.log();
console.log(c.bold(label));
let i = iterations;
while (i--) {
fn();
}

console.log(
`${iterations} iterations in ${c.bold().cyan(Date.now() - start)}ms`,
);
}

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

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

console.log();
14 changes: 14 additions & 0 deletions .benchmark/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"type": "module",
"scripts": {
"postinstall": "cd ../ && pnpm run build"
},
"dependencies": {
"arson": "^0.2.6",
"devalue": "^4.0.0",
"kleur": "^4.0.2",
"superjson": "^1.9.1",
"tupleson": "latest"
},
"devDependencies": {}
}
1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ packages:
- ./
- "examples/*"
- "examples/.*/*"
- "./benchmark"
Loading