Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pack a thing [e.g. array, object] in Javascript like in C++?

I would like to know how (and if this is possible) to pack arrays in JavaScript like in C++, because I would like to micro-optimise my code by packing an array. In C++ it looks like this:

    #pragma pack(push, 1)
    //declarations
    #pragma pack(pop)

How to replicate this behaviour in JavaScript? I would really appreciate. For knowledge on what is packing, see this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s hard to prove a negative, but I’m fairly sure nothing like that exists in JavaScript itself (I know it doesn’t in the language specification, and don’t think it does in any of the major environments for it). JavaScript has no specified memory format at all for objects or arrays (other than typed arrays, below); implementations do things their own different ways.

You could do it in a browser environment in WebAssembly (Wasm) (with C++ even), but in a standard JavaScript environment you’re a bit further removed from the bare metal than that.

For some APIs you might use a typed array (like a Uint8Array) in order to provide and receive raw bytes/words/longs, but that’s really not the same thing as #pragma pack.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading