mirror of
https://github.com/ChainSafe/lodestar.git
synced 2026-01-09 15:48:08 -05:00
**Motivation** - #7280 **Description** - Build upon the isomorphic bytes code in the utils package - refactor browser/nodejs selection to use conditional imports (like how we've been handling bun / nodejs selection - Use Uint8Array.fromHex and toHex (mentioned in https://github.com/ChainSafe/lodestar/pull/8275#issuecomment-3228184163) - Refactor the bytes perf tests to include bun - Add lodestar-bun dependency (also add missing dependency in beacon-node package) Results from my machine ``` bytes utils ✔ nodejs block root to RootHex using toHex 5500338 ops/s 181.8070 ns/op - 1048 runs 0.444 s ✔ nodejs block root to RootHex using toRootHex 7466866 ops/s 133.9250 ns/op - 2189 runs 0.477 s ✔ nodejs fromHex(blob) 7001.930 ops/s 142.8178 us/op - 10 runs 1.94 s ✔ nodejs fromHexInto(blob) 1744.298 ops/s 573.2965 us/op - 10 runs 6.33 s ✔ nodejs block root to RootHex using the deprecated toHexString 1609510 ops/s 621.3070 ns/op - 309 runs 0.704 s ✔ browser block root to RootHex using toHex 1854390 ops/s 539.2610 ns/op - 522 runs 0.807 s ✔ browser block root to RootHex using toRootHex 2060543 ops/s 485.3090 ns/op - 597 runs 0.805 s ✔ browser fromHex(blob) 1632.601 ops/s 612.5196 us/op - 10 runs 6.77 s ✔ browser fromHexInto(blob) 1751.718 ops/s 570.8683 us/op - 10 runs 6.36 s ✔ browser block root to RootHex using the deprecated toHexString 1596024 ops/s 626.5570 ns/op - 457 runs 0.805 s ✔ bun block root to RootHex using toHex 1.249563e+7 ops/s 80.02800 ns/op - 4506 runs 0.518 s ✔ bun block root to RootHex using toRootHex 1.262626e+7 ops/s 79.20000 ns/op - 3716 runs 0.409 s ✔ bun fromHex(blob) 26995.09 ops/s 37.04377 us/op - 10 runs 0.899 s ✔ bun fromHexInto(blob) 31539.09 ops/s 31.70668 us/op - 13 runs 0.914 s ✔ bun block root to RootHex using the deprecated toHexString 1.252944e+7 ops/s 79.81200 ns/op - 3616 runs 0.414 s ```
87 lines
2.0 KiB
TypeScript
87 lines
2.0 KiB
TypeScript
import {bench, describe} from "@chainsafe/benchmark";
|
|
import * as browser from "../../src/bytes/browser.ts";
|
|
import * as nodejs from "../../src/bytes/nodejs.ts";
|
|
|
|
describe("bytes utils", async () => {
|
|
const runsFactor = 1000;
|
|
const blockRoot = new Uint8Array(Array.from({length: 32}, (_, i) => i));
|
|
// FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT = 4096 * 32 = 131072
|
|
const BLOB_LEN = 131072;
|
|
const blob = new Uint8Array(BLOB_LEN);
|
|
for (let i = 0; i < blob.length; i++) {
|
|
blob[i] = i % 256;
|
|
}
|
|
const blobHex = nodejs.toHex(blob);
|
|
|
|
const implementations = [
|
|
{
|
|
name: "nodejs",
|
|
impl: nodejs,
|
|
},
|
|
{
|
|
name: "browser",
|
|
impl: browser,
|
|
},
|
|
Boolean(globalThis.Bun) && {
|
|
name: "bun",
|
|
impl: await import("../../src/bytes/bun.ts"),
|
|
},
|
|
].filter(Boolean) as {
|
|
name: string;
|
|
impl: typeof nodejs;
|
|
}[];
|
|
|
|
for (const {name, impl} of implementations) {
|
|
bench({
|
|
id: `${name} block root to RootHex using toHex`,
|
|
fn: () => {
|
|
for (let i = 0; i < runsFactor; i++) {
|
|
impl.toHex(blockRoot);
|
|
}
|
|
},
|
|
runsFactor,
|
|
});
|
|
|
|
bench({
|
|
id: `${name} block root to RootHex using toRootHex`,
|
|
fn: () => {
|
|
for (let i = 0; i < runsFactor; i++) {
|
|
impl.toRootHex(blockRoot);
|
|
}
|
|
},
|
|
runsFactor,
|
|
});
|
|
|
|
bench({
|
|
id: `${name} fromHex(blob)`,
|
|
fn: () => {
|
|
for (let i = 0; i < runsFactor; i++) {
|
|
impl.fromHex(blobHex);
|
|
}
|
|
},
|
|
runsFactor,
|
|
});
|
|
|
|
const buffer = new Uint8Array(BLOB_LEN);
|
|
bench({
|
|
id: `${name} fromHexInto(blob)`,
|
|
fn: () => {
|
|
for (let i = 0; i < runsFactor; i++) {
|
|
impl.fromHexInto(blobHex, buffer);
|
|
}
|
|
},
|
|
runsFactor,
|
|
});
|
|
|
|
bench({
|
|
id: `${name} block root to RootHex using the deprecated toHexString`,
|
|
fn: () => {
|
|
for (let i = 0; i < runsFactor; i++) {
|
|
impl.toHexString(blockRoot);
|
|
}
|
|
},
|
|
runsFactor,
|
|
});
|
|
}
|
|
});
|