Should Y.Map have toMap()?

I’m working on a Svelte-to-yjs mapping and I noticed that Y.Array has a toArray() method, but Y.Map has no corresponding toMap() method. Is this intentional?

As a side-note, I think I can get around it by using new Map({ ... map.toJSON() }) for now (not sure if that’s optimal).

Since Y.Map is an Iterable you could also do new Map(ymap) reducing some overhead.

I see several use-cases for toArray. It transforms the list representation to an Array representation. In contrast to toJSON, toArray retains type information. It only transforms the outer representation to an Array. This means that yarray.toArray()[0] will return a Y.Map if yarray.get(0) is a Y.Map. With toJSON the Y.Map will be transformed to an Objetct.

Arrays have better performance when iterating in random order than the Y.Array. Meanwhile, Y.Map and Map have similar performance. This is why there is no Y.Map yet. But if you have a real use-case for transforming to a Y.Map, then lets add toMap to the API.

2 Likes

Thanks, the toArray functionality makes more sense now.

I think for my case, new Map(ymap) is all I will need.

What do you mean “This is why there is no Y.Map yet”? I may be confused. I’m talking about transforming from a Y.Map, not to a Y.Map.

… does toArray transform into a Y.Array? (I thought it was transforming into a regular javascript Array.)

Right, I meant “this is why there is no toMap yet”.

It transforms to a native Array.