scarf
scarf•5w ago

std JSON serializer/deserializer with Set and Map support

does jsr/std have functions to serialize and deserialize sets and maps? like https://github.com/denostack/superserial but for standard library
GitHub
GitHub - denostack/superserial: A comprehensive Serializer/Deserial...
A comprehensive Serializer/Deserializer that can handle any data type. - denostack/superserial
3 Replies
Doctor 🤖
Doctor 🤖•4w ago
They dont have functions to serialise them to json but there are other serialisers to other formats. The @std/cbor package supports serialising maps but sets will need to be converted to an array first
Leokuma
Leokuma•4w ago
You can also try msgpack, but I'm not sure if it supports Set and Map There is some builtin serialization because KV supports Map and Set, but I dont think it's exposed to users: https://docs.deno.com/deploy/kv/manual/key_space/#values
Fifth-Normal-Form
Fifth-Normal-Form•4w ago
It would be very hard to beat the performance of native V8 JSON! Native V8 JSON is extremely fast
Type User = {id: number, first: string, last: string, age: number}>

// pre-fill with 100k user objects
const userMap: Map<number, User> = new Map()

// Serialize the Map
// 100k objects(10.7 MB) takes ~ 90ms.
let serializedUsers = JSON.stringify(Array.from(userMap.entries()))

/* Deserialize
* hydrating 100,000 user objects takes ~ 160ms :
* JSON.Parse: 145.30ms
* Build-Map: 16.80ms
*/
const deserializedUsers = JSON.parse(serializedUsers)
userMap = new Map(deserializedUsers)
Type User = {id: number, first: string, last: string, age: number}>

// pre-fill with 100k user objects
const userMap: Map<number, User> = new Map()

// Serialize the Map
// 100k objects(10.7 MB) takes ~ 90ms.
let serializedUsers = JSON.stringify(Array.from(userMap.entries()))

/* Deserialize
* hydrating 100,000 user objects takes ~ 160ms :
* JSON.Parse: 145.30ms
* Build-Map: 16.80ms
*/
const deserializedUsers = JSON.parse(serializedUsers)
userMap = new Map(deserializedUsers)
See this in action : https://nhrones.github.io/Hot_BuenoCache/ NOTE: First use builds a test dataset in IndexedDB. Run more than once. Repo at: https://github.com/nhrones/Hot_BuenoCache

Did you find this page helpful?