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

Time:11-26

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.

CodePudding user response:

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.

  • Related