|
| 1 | +import { readFile } from "node:fs/promises"; |
| 2 | +import { fileURLToPath } from "node:url"; |
| 3 | +import { group, bench, run } from "mitata"; |
| 4 | +import * as capnp from "capnp-es"; |
| 5 | +import protobuf from "protobufjs"; |
| 6 | + |
| 7 | +// JSON |
| 8 | +const decoder = new TextDecoder(); |
| 9 | +const jsonUrl = new URL("data/json/data.json", import.meta.url); |
| 10 | +const jsonData = await readFile(jsonUrl); |
| 11 | +const jsonObj = JSON.parse(decoder.decode(jsonData)); |
| 12 | +const jsonString = JSON.stringify(jsonObj); |
| 13 | + |
| 14 | +// Protobuf |
| 15 | +const protobufurl = new URL("data/protobuf/data.proto", import.meta.url); |
| 16 | +const protobufType = await protobuf |
| 17 | + .load(fileURLToPath(protobufurl)) |
| 18 | + .then((pb) => pb.lookupType("pi0.capnpes.test.AddressBook")); |
| 19 | +const protobufData = protobufType.encode(protobufType.create(jsonObj)).finish(); |
| 20 | + |
| 21 | +// Capnp |
| 22 | +const { AddressBook: capnpStruct } = await import("./data/capnp/data.js"); |
| 23 | +const capnpUrl = new URL("data/capnp/data-flat.bin", import.meta.url); |
| 24 | +const capnpData = await readFile(capnpUrl).then((r) => new Uint8Array(r)); |
| 25 | + |
| 26 | +// Print size table |
| 27 | +// console.table({ |
| 28 | +// JSON: jsonData.byteLength, |
| 29 | +// Capnp: capnpData.byteLength, |
| 30 | +// Protobuf: protobufData.byteLength, |
| 31 | +// }); |
| 32 | + |
| 33 | +group("parse", () => { |
| 34 | + bench("capnp.Message(<buff>).getRoot()", () => { |
| 35 | + new capnp.Message(capnpData, false, true).getRoot(capnpStruct); |
| 36 | + }); |
| 37 | + |
| 38 | + bench("protobuf.decode(<buff>)", () => { |
| 39 | + protobufType.decode(protobufData); |
| 40 | + }); |
| 41 | + |
| 42 | + bench("JSON.parse(<string>)", () => { |
| 43 | + JSON.parse(jsonString); |
| 44 | + }); |
| 45 | + |
| 46 | + bench("JSON.parse(<buff>)", () => { |
| 47 | + JSON.parse(decoder.decode(jsonData)); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +group("top level list length access", () => { |
| 52 | + bench("capnp.Message(<buff>)", () => { |
| 53 | + const message = new capnp.Message(capnpData, false, true); |
| 54 | + const addressBook = message.getRoot(capnpStruct); |
| 55 | + addressBook.getPeople().getLength().toFixed(0); |
| 56 | + }); |
| 57 | + |
| 58 | + bench("protobuf.decode(<buff>)", () => { |
| 59 | + const addressBook = protobufType.decode(protobufData); |
| 60 | + addressBook.people.length.toFixed(0); |
| 61 | + }); |
| 62 | + |
| 63 | + bench("JSON.parse(<string>)", () => { |
| 64 | + const addressBook = JSON.parse(jsonString); |
| 65 | + addressBook.people.length.toFixed(0); |
| 66 | + }); |
| 67 | + |
| 68 | + bench("JSON.parse(<buff>)", () => { |
| 69 | + const addressBook = JSON.parse(decoder.decode(jsonData)); |
| 70 | + addressBook.people.length.toFixed(0); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +group("iteration over deeply nested lists", () => { |
| 75 | + bench("capnp.Message(<buff>)", () => { |
| 76 | + const message = new capnp.Message(capnpData, false, true); |
| 77 | + const addressBook = message.getRoot(capnpStruct); |
| 78 | + // eslint-disable-next-line unicorn/no-array-for-each |
| 79 | + addressBook.getPeople().forEach((person) => { |
| 80 | + person.getId().toFixed(0); |
| 81 | + person.getName().toUpperCase(); |
| 82 | + person.getEmail().toUpperCase(); |
| 83 | + // eslint-disable-next-line unicorn/no-array-for-each |
| 84 | + person.getPhones().forEach((phone) => { |
| 85 | + phone.getNumber().toUpperCase(); |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + bench("protobuf.decode(<buff>)", () => { |
| 91 | + const addressBook = protobufType.decode(protobufData); |
| 92 | + for (const person of addressBook.people) { |
| 93 | + person.id.toFixed(0); |
| 94 | + person.name.toUpperCase(); |
| 95 | + person.email.toUpperCase(); |
| 96 | + for (const phone of person.phones) { |
| 97 | + phone.number.toUpperCase(); |
| 98 | + } |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + bench("JSON.parse(<string>)", () => { |
| 103 | + const addressBook = JSON.parse(jsonString); |
| 104 | + for (const person of addressBook.people) { |
| 105 | + person.id.toFixed(0); |
| 106 | + person.name.toUpperCase(); |
| 107 | + person.email.toUpperCase(); |
| 108 | + for (const phone of person.phones) { |
| 109 | + phone.number.toUpperCase(); |
| 110 | + } |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + bench("JSON.parse(<buff>)", () => { |
| 115 | + const addressBook = JSON.parse(decoder.decode(jsonData)); |
| 116 | + for (const person of addressBook.people) { |
| 117 | + person.id.toFixed(0); |
| 118 | + person.name.toUpperCase(); |
| 119 | + person.email.toUpperCase(); |
| 120 | + for (const phone of person.phones) { |
| 121 | + phone.number.toUpperCase(); |
| 122 | + } |
| 123 | + } |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +await run(); |
0 commit comments