Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory layout in Javascript - data-oriented vs object-oriented design [closed]

Coming from a background of C/C++, memory layout of objects with regards to reducing cache misses is something that is crucial especially when working on consoles. Data-oriented design is often favored over object-oriented design, in order to help keep related objects close to each other in memory (especially in performance critical areas).

Recently, I've been doing some Javascript development, and I'm wondering what the general consensus is within the Javascript community.

With my limited experience in Javascript, I've often been surprised to see completely unexpected results when profiling. The internal memory layout and implementation of Javascript objects/structures varies so greatly from browser to browser, that I wonder if it is worth the effort to attempt to optimize.

I created a simple test case (http://jsperf.com/object-vs-data) on jsPerf to compare the performance of the two methods, and while it shows performance gains on Chrome, there is no noticeable speedup on Safari.

In Javascript, should I even concern myself with the memory layout of objects? Or is it more of a 'implement it one way and then optimize if needed' type thing?

This second option seems kind of wasteful (in terms of development time), especially if there is some good guideline to follow.

Thanks~

Supplemental Information: This is basically how I would implement the two approaches in Javascript. The jsPerf test case above is implemented like this.

var objectOriented = [
    { foo: 1, bar: 2 },
    { foo: 3, bar: 4 }
];

var dataOriented = {
    foos: [1, 3],
    bars: [2, 4]
};

// Object-oriented access:
var a = objectOriented[0].bar;

// Data-oriented access:
var b = dataOriented.bars[0];
like image 877
smg Avatar asked Jul 30 '14 15:07

smg


People also ask

What is DOP vs OOP?

In object-oriented programming you are focusing on a single object (class - its methods, members, etc.). In data-oriented design you are thinking how data is touched and processed. You just have a box that processes your input data to your output data (the ideal input data is the same as output).

Is JavaScript fully object-oriented?

JavaScript is not a class-based object-oriented language. But it still has ways of using object oriented programming (OOP).

What is object oriented design in JavaScript?

Object-Oriented Programming is a way of writing code that allows you to create different objects from a common object. The common object is usually called a blueprint while the created objects are called instances. Each instance has properties that are not shared with other instances.

What is meant by Object Oriented Design?

Object-oriented design (OOD) is the process of using an object-oriented methodology to design a computing system or application. This technique enables the implementation of a software solution based on the concepts of objects.


1 Answers

You're working from a fundamental assumption that objects in Javascript work like they do in C++. They don't.

In C++, the primary purpose of a type is to act as a "lens" over a chunk of memory. The class layout directly defines the contents of the memory that the object describes, in a well defined way. C/C++ arrays specifically require a linear, continuous layout of homogeneous types.

In JavaScript, an object is a collection of name/value pairs. An array is just an object with a special "length" property. Note that there's NO description or definition of memory layout here. There's nothing stopping a Javascript interpreter from implementing arrays as a hash table rather than a linear chunk of memory; in fact, I'm sure they are JS implementations that do just that.

JavaScript implementations are free to lay out memory however they want, and there's no correspondence between anything you do in source and what actually ends up in the machine.

In addition, JavaScript arrays are heterogeneous, not homogeneous. That is, assuming that it was laid out in contiguous memory, your equivalent type in C would be JSObject **, not int ** (or float ** or whatever). A JS array is a collection of references to data stored elsewhere, so even if the references were in your cache line, your data won't be.

So, in summary - this kind of thinking will gain you nothing but pain. JavaScript is a much higher level language than C++, and part of that is giving up the control you're used to. That kind of low-level optimization if possible will be done by the interpreter. Concentrate on writing code with efficient algorithms that naturally express your solution; that's hard enough as it is. :-)

like image 177
Chris Tavares Avatar answered Sep 24 '22 02:09

Chris Tavares