|
| 1 | +import { Bench } from 'tinybench'; |
| 2 | +import { withCodSpeed } from '@codspeed/tinybench-plugin'; |
| 3 | +import * as r from 'rkyv-js'; |
| 4 | + |
| 5 | +const bench = withCodSpeed(new Bench()); |
| 6 | + |
| 7 | +const testU8 = 255; |
| 8 | +const testI8 = -128; |
| 9 | +const testU16 = 65535; |
| 10 | +const testI16 = -32768; |
| 11 | +const testU32 = 4294967295; |
| 12 | +const testI32 = -2147483648; |
| 13 | +const testU64 = 18446744073709551615n; |
| 14 | +const testI64 = -9223372036854775808n; |
| 15 | +const testF32 = 3.14159; |
| 16 | +const testF64 = 3.141592653589793; |
| 17 | +const testBool = true; |
| 18 | +const testChar = '🦀'; |
| 19 | +const testStringShort = 'Hello'; |
| 20 | +const testStringLong = 'Hello, World! This is a longer string that exceeds inline storage.'; |
| 21 | + |
| 22 | +const u8Bytes = r.encode(r.u8, testU8); |
| 23 | +const i8Bytes = r.encode(r.i8, testI8); |
| 24 | +const u16Bytes = r.encode(r.u16, testU16); |
| 25 | +const i16Bytes = r.encode(r.i16, testI16); |
| 26 | +const u32Bytes = r.encode(r.u32, testU32); |
| 27 | +const i32Bytes = r.encode(r.i32, testI32); |
| 28 | +const u64Bytes = r.encode(r.u64, testU64); |
| 29 | +const i64Bytes = r.encode(r.i64, testI64); |
| 30 | +const f32Bytes = r.encode(r.f32, testF32); |
| 31 | +const f64Bytes = r.encode(r.f64, testF64); |
| 32 | +const boolBytes = r.encode(r.bool, testBool); |
| 33 | +const charBytes = r.encode(r.char, testChar); |
| 34 | +const stringShortBytes = r.encode(r.string, testStringShort); |
| 35 | +const stringLongBytes = r.encode(r.string, testStringLong); |
| 36 | + |
| 37 | +bench.add('primitives/decode - u8', () => { |
| 38 | + r.decode(r.u8, u8Bytes); |
| 39 | +}); |
| 40 | + |
| 41 | +bench.add('primitives/decode - i8', () => { |
| 42 | + r.decode(r.i8, i8Bytes); |
| 43 | +}); |
| 44 | + |
| 45 | +bench.add('primitives/decode - u16', () => { |
| 46 | + r.decode(r.u16, u16Bytes); |
| 47 | +}); |
| 48 | + |
| 49 | +bench.add('primitives/decode - i16', () => { |
| 50 | + r.decode(r.i16, i16Bytes); |
| 51 | +}); |
| 52 | + |
| 53 | +bench.add('primitives/decode - u32', () => { |
| 54 | + r.decode(r.u32, u32Bytes); |
| 55 | +}); |
| 56 | + |
| 57 | +bench.add('primitives/decode - i32', () => { |
| 58 | + r.decode(r.i32, i32Bytes); |
| 59 | +}); |
| 60 | + |
| 61 | +bench.add('primitives/decode - u64', () => { |
| 62 | + r.decode(r.u64, u64Bytes); |
| 63 | +}); |
| 64 | + |
| 65 | +bench.add('primitives/decode - i64', () => { |
| 66 | + r.decode(r.i64, i64Bytes); |
| 67 | +}); |
| 68 | + |
| 69 | +bench.add('primitives/decode - f32', () => { |
| 70 | + r.decode(r.f32, f32Bytes); |
| 71 | +}); |
| 72 | + |
| 73 | +bench.add('primitives/decode - f64', () => { |
| 74 | + r.decode(r.f64, f64Bytes); |
| 75 | +}); |
| 76 | + |
| 77 | +bench.add('primitives/decode - bool', () => { |
| 78 | + r.decode(r.bool, boolBytes); |
| 79 | +}); |
| 80 | + |
| 81 | +bench.add('primitives/decode - char', () => { |
| 82 | + r.decode(r.char, charBytes); |
| 83 | +}); |
| 84 | + |
| 85 | +bench.add('primitives/decode - string (short)', () => { |
| 86 | + r.decode(r.string, stringShortBytes); |
| 87 | +}); |
| 88 | + |
| 89 | +bench.add('primitives/decode - string (long)', () => { |
| 90 | + r.decode(r.string, stringLongBytes); |
| 91 | +}); |
| 92 | + |
| 93 | +await bench.run(); |
| 94 | +console.table(bench.table()); |
0 commit comments