Timeline for How do you JSON.stringify an ES6 Map?
Current License: CC BY-SA 3.0
17 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Feb 19 at 9:10 | comment | added | VLAZ | "You can't. The keys of a map can be anything" that's only true in the most generic possible case when the programmer has no knowledge or expectation of the data being serialised. However, in the exceptionally vast majority or cases, it's entirely possible, moreover, trivial to implement a serialisation for maps. Since you know what the data is, it's not hard to create an extremely simple serialise/deserialise pair that will work. Where it might get hard is if an object key holds more than their key/values. E.g. it's a class instance or a function. But it'd be dubious to serialise that. | |
Feb 2, 2023 at 22:05 | comment | added | Andrew | @Stefnotch imgur.com/a/cba5XXO | |
Jul 28, 2022 at 15:52 | comment | added | Stefnotch | While this answer definitely points out the tricky bits, it most certainly is not "impossible" as the accepted answer demonstrates. | |
Aug 10, 2020 at 14:38 | comment | added | napolux | Just passing by and figure out my problem thanks to this. I really wish to move to a farm and leave all this behind, sometimes. | |
Nov 6, 2019 at 9:05 | comment | added | bvdb |
A Map uses buckets to organize its keys. So, it should be faster than a simple property, especially when you have 1000 of them.
|
|
Sep 28, 2018 at 21:08 | comment | added | 4thex |
I am a little late to the party, but I had a similar need, and I was able to do this: JSON.stringify(Array.from(myMap.entries()))
|
|
Mar 18, 2016 at 19:52 | comment | added | Xplouder | True, seems that Firefox 45v iterates objects away faster than Chrome +49v. However Maps still wins vs objects in Chrome. | |
Mar 18, 2016 at 13:49 | comment | added | Oriol |
@Xplouder That test uses expensive hasOwnProperty . Without that, Firefox iterates objects much faster than maps. Maps are still faster on Chrome, though. jsperf.com/es6-map-vs-object-properties/95
|
|
Mar 18, 2016 at 2:32 | comment | added | Xplouder | Map > Object in performance scope - jsperf.com/es6-map-vs-object-properties/94 | |
Feb 11, 2016 at 18:13 | comment | added | Lilleman | @Oriol Cool. I'm imagining the key always just being a pointer to a more advanced object anyways, so it should, in my head, always be optimized. In theory. I'll research this a bit I think. :) | |
Feb 11, 2016 at 18:04 | comment | added | Oriol | @Lilleman I remember reading that a certain version of Firefox had the improvement of detecting when all keys in a map are strings, then it could be optimized more (like objects). So I guess maps will be slower on browsers without this kind of optimization. But that's implementation-dependent, and I haven't done any test to measure performance. | |
Feb 11, 2016 at 18:01 | comment | added | Lilleman | "It might be faster" - Do you have any source on that? I'm imagining a simple hash-map must be faster than a full blown object, but I have no proof. :) | |
Jan 29, 2016 at 6:20 | comment | added | Oriol | I have explained here what exactly I meant when I said "you can't". | |
Jan 6, 2016 at 16:20 | comment | added | Capaj | for the curious-in the latest chrome, any map serializes into '{}' | |
Mar 16, 2015 at 19:42 | history | edited | rynop | CC BY-SA 3.0 |
ninja
|
Mar 16, 2015 at 19:41 | vote | accept | rynop | ||
Oct 9, 2020 at 18:06 | |||||
Mar 16, 2015 at 19:34 | history | answered | Oriol | CC BY-SA 3.0 |