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

Commit

Permalink
feat: Add native JSON to benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Akxe committed Oct 27, 2023
1 parent a277423 commit 594843a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
15 changes: 14 additions & 1 deletion benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import * as superjson from "superjson";
import { createTson, tsonDate, tsonRegExp, tsonSet } from "tupleson";
import { gzipSync } from "zlib";

import { nativeJSONParse, nativeJSONStringify } from './native-json/index.js';

const obj = {
array: [{ foo: 1 }, { bar: 2 }, { baz: 3 }],
date: new Date(),
number: 42,
regex: /the quick brown fox/,
regex: /the quick brown fox/i,
set: new Set([1, 2, 3]),
xss: '</script><script>alert("XSS")</script>',
};
Expand All @@ -25,6 +27,17 @@ const tson = createTson({
});

const testingMethods = [
{
name: 'native',
parse: str => nativeJSONParse(str),
results: {
parse: 0,
size: 0,
sizeGZipped: 0,
stringify: 0,
},
stringify: () => nativeJSONStringify(obj),
},
{
name: 'superJSON',
parse: str => superjson.parse(str),
Expand Down
2 changes: 2 additions & 0 deletions benchmark/native-json/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './stringify.js';
export * from './parser.js';
33 changes: 33 additions & 0 deletions benchmark/native-json/parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export function nativeJSONParse(value) {
return JSON.parse(value, reviver);
}

function dateReviver(value) {
return new Date(value.iso);
}

function regExpReviver(value) {
return new RegExp(value.source, value.flags);
}

function setReviver(value) {
return new Set(value.values);
}

function reviver(key, value) {
if (typeof value === 'object' && value !== null) {
if ('__type' in value) {
switch (value.__type) {
case 'Date':
return dateReviver(value);
case 'RegExp':
return regExpReviver(value);
case 'Set':
return setReviver(value);
}
}
}

// For primitives, use native function
return value;
}
50 changes: 50 additions & 0 deletions benchmark/native-json/stringify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export function nativeJSONStringify(value) {
return JSON.stringify(value, replacer);
}

function dateReplacer(value) {
return {
__type: 'Date',
iso: value.toISOString(),
};
}

function regExpReplacer(value) {
return {
__type: 'RegExp',
flags: value.flags,
source: value.source,
};
}

function setReplacer(value) {
return {
__type: 'Set',
values: Array.from(value),
};
}

function replacer(key, value) {
if (typeof value === 'bigint') {
return value.toString();
} else if (typeof value === 'symbol') {
return value.toString();
} else if (typeof value === 'object') {
if (value instanceof Date) {
return dateReplacer(value);
} else if (value instanceof RegExp) {
return regExpReplacer(value);
} else if (value instanceof Set) {
return setReplacer(value);
}

return value;
} else if (typeof value === 'string') {
if (this[key] instanceof Date) {
return dateReplacer(this[key]);
}
}

// For primitives, use native function
return value;
}

0 comments on commit 594843a

Please sign in to comment.