/* TableJSON vs JSON 性能对比 (深度优化后) ======================================== [stringify] 简单对象: JSON.stringify: 3,985,548 ops/s | fastest TableJSON.stringify: 2,125,028 ops/s | 46.68% slower (初版: 95.12% slower) [parse] 简单对象: TableJSON.parse: 1,181,367 ops/s | fastest (反超 JSON.parse!) JSON.parse: 1,113,568 ops/s | 5.74% slower [stringify] 大数组 (1000 items): JSON.stringify: 6,406 ops/s | fastest TableJSON.stringify: 4,729 ops/s | 26.18% slower (初版: 90.81% slower) [parse] 大数组 (1000 items): JSON.parse: 4,582 ops/s | fastest TableJSON.parse: 3,095 ops/s | 32.45% slower [stringify] 深层嵌套 (50 levels): JSON.stringify: 365,780 ops/s | fastest TableJSON.stringify: 222,896 ops/s | 39.06% slower (初版: 92.48% slower) 性能提升总结 (stringify): ┌─────────────────────┬────────────┬────────────┬────────────┐ │ 场景 │ 初版 │ 深度优化后 │ 提升倍数 │ ├─────────────────────┼────────────┼────────────┼────────────┤ │ 简单对象 │ 95% slower │ 47% slower │ ~11x │ │ 大数组 (1000 items) │ 91% slower │ 26% slower │ ~8x │ │ 深层嵌套 (50层) │ 92% slower │ 39% slower │ ~7x │ └─────────────────────┴────────────┴────────────┴────────────┘ 核心优化手段: 1. Map O(1) 查找替代数组线性查找 2. constructor.name 快速类型匹配 3. 纯数据对象快速检测 + 直接使用 JSON.stringify 4. 特殊类型直接生成 JSON 字符串(stringifyDirect) 5. 避免创建中间对象,直接拼接字符串 */ import b from "benny" import { TableJSON } from "../funcs/TableJSON" // ===================== 测试数据准备 ===================== // 简单对象(JSON 原生支持) const simpleObject = { name: "test", age: 25, active: true, tags: ["a", "b", "c"], nested: { x: 1, y: 2, z: [1, 2, 3], }, } // 复杂对象(包含 JSON 不支持的类型) const complexObject = { name: "test", createdAt: new Date("2024-01-15T10:30:00.000Z"), pattern: /test\d+/gi, metadata: new Map([ ["key1", "value1"], ["key2", 123], ["key3", { nested: true }], ]), tags: new Set(["tag1", "tag2", "tag3"]), bigId: 9007199254740991n, buffer: new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]), nested: { dates: [new Date("2023-01-01"), new Date("2023-12-31")], numbers: new Set([1, 2, 3, 4, 5]), }, } // 大数组 const largeArray = Array.from({ length: 1000 }, (_, i) => ({ id: i, name: `item-${i}`, value: Math.random(), active: i % 2 === 0, })) // 大数组(带复杂类型) const largeArrayComplex = Array.from({ length: 1000 }, (_, i) => ({ id: BigInt(i), name: `item-${i}`, createdAt: new Date(Date.now() - i * 1000), tags: new Set([`tag-${i}`, `tag-${i + 1}`]), })) // 深层嵌套对象 function createDeepObject(depth: number): any { if (depth === 0) return { value: "leaf" } return { child: createDeepObject(depth - 1), level: depth } } const deepObject = createDeepObject(50) // 预先序列化的字符串 const simpleObjectJson = JSON.stringify(simpleObject) const simpleObjectTableJson = TableJSON.stringify(simpleObject) const complexObjectTableJson = TableJSON.stringify(complexObject) const largeArrayJson = JSON.stringify(largeArray) const largeArrayTableJson = TableJSON.stringify(largeArray) const largeArrayComplexTableJson = TableJSON.stringify(largeArrayComplex) const deepObjectJson = JSON.stringify(deepObject) const deepObjectTableJson = TableJSON.stringify(deepObject) // ===================== Benchmark Suites ===================== // 1. 简单对象序列化对比 b.suite( "[stringify] 简单对象 - TableJSON vs JSON", b.add("JSON.stringify", () => { JSON.stringify(simpleObject) }), b.add("TableJSON.stringify", () => { TableJSON.stringify(simpleObject) }), b.cycle(), b.complete() ) // 2. 简单对象解析对比 b.suite( "[parse] 简单对象 - TableJSON vs JSON", b.add("JSON.parse", () => { JSON.parse(simpleObjectJson) }), b.add("TableJSON.parse (from JSON string)", () => { TableJSON.parse(simpleObjectJson) }), b.add("TableJSON.parse (from TableJSON string)", () => { TableJSON.parse(simpleObjectTableJson) }), b.cycle(), b.complete() ) // 3. 复杂对象(TableJSON 专属功能) b.suite( "[stringify/parse] 复杂对象 (Date, Map, Set, bigint, TypedArray)", b.add("TableJSON.stringify", () => { TableJSON.stringify(complexObject) }), b.add("TableJSON.parse", () => { TableJSON.parse(complexObjectTableJson) }), b.cycle(), b.complete() ) // 4. 大数组序列化 b.suite( "[stringify] 大数组 (1000 items) - TableJSON vs JSON", b.add("JSON.stringify", () => { JSON.stringify(largeArray) }), b.add("TableJSON.stringify", () => { TableJSON.stringify(largeArray) }), b.cycle(), b.complete() ) // 5. 大数组解析 b.suite( "[parse] 大数组 (1000 items) - TableJSON vs JSON", b.add("JSON.parse", () => { JSON.parse(largeArrayJson) }), b.add("TableJSON.parse", () => { TableJSON.parse(largeArrayTableJson) }), b.cycle(), b.complete() ) // 6. 大数组(带复杂类型) b.suite( "[stringify/parse] 大数组带复杂类型 (1000 items with bigint, Date, Set)", b.add("TableJSON.stringify", () => { TableJSON.stringify(largeArrayComplex) }), b.add("TableJSON.parse", () => { TableJSON.parse(largeArrayComplexTableJson) }), b.cycle(), b.complete() ) // 7. 深层嵌套对象 b.suite( "[stringify] 深层嵌套对象 (50 levels) - TableJSON vs JSON", b.add("JSON.stringify", () => { JSON.stringify(deepObject) }), b.add("TableJSON.stringify", () => { TableJSON.stringify(deepObject) }), b.cycle(), b.complete() ) // 8. 深层嵌套对象解析 b.suite( "[parse] 深层嵌套对象 (50 levels) - TableJSON vs JSON", b.add("JSON.parse", () => { JSON.parse(deepObjectJson) }), b.add("TableJSON.parse", () => { TableJSON.parse(deepObjectTableJson) }), b.cycle(), b.complete() ) // 9. 单个类型序列化性能 const testDate = new Date() const testMap = new Map([["a", 1], ["b", 2], ["c", 3]]) const testSet = new Set([1, 2, 3, 4, 5]) const testBigInt = 9007199254740991n const testUint8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) b.suite( "[stringify] 单个特殊类型", b.add("Date", () => { TableJSON.stringify(testDate) }), b.add("Map (3 entries)", () => { TableJSON.stringify(testMap) }), b.add("Set (5 items)", () => { TableJSON.stringify(testSet) }), b.add("bigint", () => { TableJSON.stringify(testBigInt) }), b.add("Uint8Array (10 bytes)", () => { TableJSON.stringify(testUint8Array) }), b.cycle(), b.complete() ) // 预序列化单个类型 const dateJson = TableJSON.stringify(testDate) const mapJson = TableJSON.stringify(testMap) const setJson = TableJSON.stringify(testSet) const bigintJson = TableJSON.stringify(testBigInt) const uint8ArrayJson = TableJSON.stringify(testUint8Array) b.suite( "[parse] 单个特殊类型", b.add("Date", () => { TableJSON.parse(dateJson) }), b.add("Map (3 entries)", () => { TableJSON.parse(mapJson) }), b.add("Set (5 items)", () => { TableJSON.parse(setJson) }), b.add("bigint", () => { TableJSON.parse(bigintJson) }), b.add("Uint8Array (10 bytes)", () => { TableJSON.parse(uint8ArrayJson) }), b.cycle(), b.complete() )