-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Side effects false #68
Conversation
I tested the bundle size difference for so I going to close this PR |
This is still worth doing. It's not that it will make this library smaller, it's that if Without |
yes, but there is a side effect: |
The side effect being the thrown error? |
yes, thrown an error on a module level |
If all of |
So, how should the app behave on unsupported architectures then? Are you suggesting its methods should just corrupt data? |
In the case described here the whole library would be reaped by the compiler. There would be no methods left to call. |
I don't see how we can have one thing without another. Runtime in-hash checks for little-endianness can't be done, because that would screw up perf - which was very thoroughly tested. |
Here's an example. import { bytesToHex } from '@noble/hashes/utils';
export function getRandomBytes() {
const bytes = // Get some random bytes however you like.
if (__DEV__) {
console.log(`Generated random bytes: ${bytesToHex(bytes)}`);
}
return bytes;
} When your compiler injects import { bytesToHex } from '@noble/hashes/utils';
export function getRandomBytes() {
const bytes = // Get some random bytes however you like.
console.log(`Generated random bytes: ${bytesToHex(bytes)}`);
return bytes;
} When your compiler injects import { bytesToHex } from '@noble/hashes/utils';
export function getRandomBytes() {
const bytes = // Get some random bytes however you like.
return bytes;
} Even though every bit of If you add export function getRandomBytes() {
const bytes = // Get some random bytes however you like.
return bytes;
} |
@steveluscher I hear you, but you don't hear me. Adding |
I'm trying! I don't understand why the This is separate from my general inclination for module factories to be side-effect free, which you know by now to predict from me, when you said this:
That's fair! I'd be curious if you could preserve the library's performance characteristics by doing something like this: function getEndianness() {
return new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44
? 'le'
: 'be';
}
let endianness: 'be' | 'le' | undefined;
function hashStuff() {
if ((endianness ||= getEndianness()) !== 'le') {
throw new Error('Non little-endian hardware is not supported');
}
// Produce hash…
} In any case, just adding |
As far as I understood, @jeetiss mentioned that adding |
In short, |
If what you're saying is also true for other bundlers, then I see zero reasons not to add |
Thank you! That's all I came here to achieve. |
Do you want to put this PR back up @jeetiss, so that Paul can merge it, or would you like me to? |
this is my second take on how to make library side effects free. (first one #63)