简体   繁体   中英

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 .

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM