lodestar
lodestar copied to clipboard
Review `Buffer.from` usage
Review how Buffer.from is used within Lodestar, and check the following:
- is it required to convert to
Bufferat all? - can we convert to
Bufferwithout memory copy? - does it impact browser compatibility?
Eg. a good example of incorrect Buffer.from usage
https://github.com/ChainSafe/lodestar/blob/252fd2f50a7e759d53983286dcd89f9e7e8d5f60/packages/validator/src/slashingProtection/block/blockBySlotRepository.ts#L57
Which either should not be a Buffer at all as based on type a Uint8Array is valid, or if Buffer is required should avoid the memory copy.
Depending on the size of the Uint8Array, converting to Buffer with memory copy is quite expensive while without copy it's essentially free
From https://github.com/ChainSafe/lodestar/compare/unstable...nflaig/bench-buffer-from
Buffer utils
✔ Buffer.from - copy 7.286412 ops/s 137.2418 ms/op x1.024 38 runs 5.82 s
✔ Buffer.from - no copy 1117318 ops/s 895.0000 ns/op x0.982 723013 runs 1.11 s
✔ Buffer.from - no copy with offset 1179245 ops/s 848.0000 ns/op - 2699558 runs 3.85 s
Related https://github.com/ChainSafe/lodestar/pull/6687