ipld / js-block

IPLD Block Interface
6 stars 3 forks source link

feat: make encode/decode safe and add encodeUnsafe/decodeUnsafe #2

Closed mikeal closed 4 years ago

mikeal commented 4 years ago

This is a change I’ve been meaning to make for a while.

Before this change, we didn’t actually cache decodes and suffered a full encode/decode in order to get a copy. We also cached the encode and exposed it directly, which is a mutable buffer so it was quite unsafe.

With this change:

This means that the default encode()/decode() will be noticable slower, but if you are sure the result won’t be mutated you can use the unsafe methods and get better performance than prior versions with full caching.

This is similar to Node.js Buffer’s approach to performance and safety.

mikeal commented 4 years ago

Demonstrates the need for immutable TypedArrays

Yup, I logged an issue in nodejs/node for that feature while doing the initial implementation. Writing this library definitely made me feel like we don’t have enough immutable primitives in JS across the board.