Bnaya / objectbuffer

JavaScript Object like api, backed by an arraybuffer
MIT License
171 stars 5 forks source link

Reuse strings and maintain objects references on save #102

Closed Bnaya closed 4 years ago

Bnaya commented 4 years ago

Currently, when we save a value, even when there are identical strings, numbers, and objects that are reference to the same object, they will be saved as new values and take additional memory. That's true for also object keys.

We may add map that let us save the value once and use the same saved value pointer for all of the occurrences. But we will need to have reference counting and "copy and write" behaviour as we have now for objects. due to the overhead of the reference counting, it might be better to not apply it to numbers and bigint

examples:

Instead of creating 2 string entries ["a", "a"] We will create one, with reference count 2. When you'll have an array of objects, that can save a lot of memory by not re-saving the objects keys for each object instance

[{
  "postId": 1,
  "id": 1,
  "name": "id labore ex et quam laborum",
  "email": "Eliseo@gardner.biz",
  "body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium"
},
{
  "postId": 1,
  "id": 1,
  "name": "id labore ex et quam laborum",
  "email": "Eliseo@gardner.biz",
  "body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium"
}.
...
]

Another optimisation would be: preserving this kind of cache for the lifetime of the objectbuffer, That can open many interesting things, to be elaborated on another issue

Bnaya commented 4 years ago

Done :)