Giter Club home page Giter Club logo

Comments (6)

emibcn avatar emibcn commented on July 30, 2024

Hi! Have you done some profiling/benchmarking? If yes, what are the results you've got?

I've just published a library similar to this one (@3m1/binary-object), but with fixed schema defined using in-class @decorators. I archived a 2-3 times slower instantiation/read/write operations compared to normal JS objects, with an extra 2-3 times heap memory usage on instantiation. If this is good for you, I can take a look into this issue and, if I'm able, apply here some of the work done in my lib.

from objectbuffer.

Bnaya avatar Bnaya commented on July 30, 2024

Hey @emibcn :)
There's automatic self-benchmarks on each PR:
#146 (comment)
For various factors, IMO we can't effectively compare this library to native js objects.

Anyhow, I would love to collaborate about this topic!

I think that the "free shape" + automatic memory reclaim brings many difficulties
I will throw in some of the challenges to make fast props, or cheap objects:
We will need to have an objects shape registry, a way to compute object registry, and a cheap way to search the registry to see if this share is already exists.
(every shape will need to have reference count so we know when we can re claim it)
And a very painful code path that we will must have is, when object shape changes, we will need to turn it into slow object, or assign it a new shape.
All that is a lot of code, with unclear performance benefits (yet)

from objectbuffer.

emibcn avatar emibcn commented on July 30, 2024

I think that the "free shape" + automatic memory reclaim brings many difficulties

Me too. This is why I did mine without automatic memory and fixed shape 😜 Well, not only because of complexity, but also versatility. And maybe this would be a solution: let the user decide how to manage the object via some parameters:

We will need to have an objects shape registry, a way to compute object registry, and a cheap way to search the registry to see if this share is already exists.
(every shape will need to have reference count so we know when we can re claim it)

This is going to be a JS re-programming, but using JS itself, which performance is not always as expected. Have you considered adding some WASM to this project?

And a very painful code path that we will must have is, when object shape changes, we will need to turn it into slow object, or assign it a new shape.

Here is when we can let the user decide, using some of the options as default: it's not the same to handle a bunch of objects changing very fast than millions of objects rarely changing.

All that is a lot of code, with unclear performance benefits (yet)

I have taken several dead-end paths because of performance. But I had lot of fun walking those paths ;)

from objectbuffer.

moomoolive avatar moomoolive commented on July 30, 2024

Hey @Bnaya ,

I know I'm late to the party, but I think I've figured out a way create very fast objects that are backed by buffers.

Essentially what you do is create classes on the fly via the unsafe Function constructor that maps key names to a particular offsets in a typed array via getters and setters.

Here's an example:

// your object definition
const def = {x: "num", y: "num"}

// create a class based on definition
const MyObject = Function(`return class MyBufferObject {
  constructor() {
    this._memory = new Float64Array(${Object.keys(def).length})
  }

  ${Object.keys(def).map((key, offset) => {
    const getter = `get ${key}() { return this._memory[${offset}] }`
    const setter = `set ${key}(newValue) { this._memory[${offset}] = newValue }`
    return getter + ";" + setter
  }).join("\n")}
}
`)()

// now you can use it like a normal object
const obj = new MyObject()
obj.x = 2
obj.y = 5
const {x, y} = obj
console.log(x, y) // output: 2 5

From my benchmarking, objects created this way are just as fast as native objects.

Anyhow, I though I'd give you my two cents on the issue.

from objectbuffer.

Bnaya avatar Bnaya commented on July 30, 2024

@moomoolive thank you for the research!
The library supports dynamic object shape which means we can't ahead of time create fixed getters and settres, among other things.

from objectbuffer.

moomoolive avatar moomoolive commented on July 30, 2024

Fair enough! This approach wouldn't really work with dynamic objects.

PS this library is such a good idea - I really hope js supports something like this out of the box in the future

from objectbuffer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.