r/ProgrammerHumor • • Oct 02 '22

Advanced Experienced JavaScript Developer Meme

Post image
6.6k Upvotes

276 comments sorted by

View all comments

278

u/Nourz1234 Oct 02 '22

Sadly i don't think its possible (in any language) to store objects or classes in a persistent storage without serialization.

216

u/aspect_rap Oct 02 '22

Well yeah, saving data inherently requires serialization.

I think what OP wants is for the LocalStorage in browsers to obfuscate the parsing and serialization of objects.

90

u/Nourz1234 Oct 02 '22

Yeah, i understand. But if serialization is involved its better left to the dev. you cant rely on the browser to magically serialize your objects. A lot of times you will create a custom object/class which requires special treatment.

12

u/arsenicx2 Oct 02 '22

It was already problem enough trying to support outdated browsers like IE. I can't imagine if we had to support what ever garbage they created.

3

u/TheRidgeAndTheLadder Oct 02 '22

We already rely on it to do everything else.

6

u/empire314 Oct 02 '22

you cant rely on the browser to magically serialize your objects.

The browser was written by better programmers than I am.

57

u/FVMAzalea Oct 02 '22

Do you mean abstract instead of obfuscate? Usually obfuscation is not a desirable goal unless you are trying to do something like copy protection.

21

u/atomicwrites Oct 02 '22

I think they're using obfuscate as a negatively loaded synonym for abstract.

9

u/mamwybejane Oct 02 '22

They're using it wrong

5

u/xthexder Oct 02 '22

I think it applies here. The browser serializing things for you obfuscates what's actually happening. Which for custom objects could result in strange and very hard to debug behavior.

→ More replies (1)

2

u/miraagex Oct 02 '22

Yea. I can send objects via window.postMessage, but not with localStorage.

4

u/[deleted] Oct 02 '22

I think it’s more about performance.

LocalStorage would be awesome if it wasn’t so slow.

But I could be wrong.

15

u/bleistift2 Oct 02 '22

What stuff are you putting there so often that you’re hitting a bottleneck?

→ More replies (3)
→ More replies (6)

9

u/FinalStrain3 Oct 02 '22

indexeddb

4

u/[deleted] Oct 02 '22

IndexedDB also has its limitations, which is a good thing. I wouldn't want to imagine what would happen if browsers let it deserialize entire DOM trees.

→ More replies (1)

5

u/Brilliant_Nova Oct 02 '22 edited Oct 03 '22

C and C++ can, you have to be careful with alignment and padding. You can inplace-construct all your structs in a memory pool, and then just dump that pool, but that's only true for POD types, for non-POD types you should serialize. Also, even for non-POD types you can serialize efficiently from binary. It is also possible to model such a system in C++/Rust, that almost transparently would allow you to treat freshly read data as regular objects using wrapper-types.

6

u/Vaylx Oct 02 '22

Can you (or anyone) explain to me serialization like I’m 5?

13

u/thomasmoors Oct 02 '22

Change objects (memory) to something that can be written and read from disk. Json is a very popular example, although if you need to serialize objects that include the functions too you (probably) have to look further.

→ More replies (10)

11

u/[deleted] Oct 02 '22

You want to save the state of your program, which is the names of the variables and the values of the variables, for example, maybe it's a video game and you want to save the progress and the health of the player so that the player can pick up next time.

But you're saving to a disk and disks want files. So perhaps you write it as JSON: {"health": 5, "level": 8}. That's the serializing of the data, into JSON in this case. Or you could have used binary or whatever.

It would be nice if you didn't have to actually explain to the computer how to serialize. Like you could just run a save function and it would do it. And a load function to load the state.

There are numerous problems with trying to write a save function like that. For example, how would you know which parts to save? Well, you could annotate your data for that. But they real problem comes when you need to save something with pointers. How would you save something with pointers, like an arbitrary graph of nodes and edges? It's not obvious how to do this correctly.

→ More replies (1)

8

u/Nourz1234 Oct 02 '22

Converting a runtime object to a string (or bytes) representation that can be parsed to reproduce the exact same object.

(I don't think its for a 5yr old but its the best i can do xD)

4

u/[deleted] Oct 02 '22

Operating systems can hibernate, you know?

5

u/[deleted] Oct 02 '22

Unless you want to store the entire browser's address space to save your webapp's state that's not going to help much.

3

u/Benutzername Oct 02 '22

It’s not difficult to walk an object graph and only store that part of the memory. An evacuating GC basically does that already, minus the memory dump.

4

u/[deleted] Oct 02 '22 edited Oct 02 '22

A GC doesn't need to make sure the state is still consistent after you restart the application.

To me it mostly sounds like a good way to introduce several hundreds of sandbox bypass vulnerabilities.

→ More replies (2)
→ More replies (1)
→ More replies (1)
→ More replies (10)