开发者

Memory question in javascript

Every time you instantiate a object or a function, does it store memory or is just variables with primitive values that are stored.

For E开发者_运维问答xample - var myArray = new Array(); or var num = function(){};


When you do

var myArray = new Array();

You create an object in memory and store a reference to that object in the myArray variable. (A reference is like a pointer in C/C++. It's a value that tells the JavaScript engine where the object is.) E.g.:

+−−−−−−−−−+           
| myArray |           
+−−−−−−−−−+           +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
|   ref   |−−−−−−−−−−>| instance of an array       |
+−−−−−−−−−+           +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | length = 0                 |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+

When you do:

var n = 2;

You store the actual value 2 in n:

+−−−−−−−−−+
|    n    |
+−−−−−−−−−+
|    2    |
+−−−−−−−−−+

This is the essential difference between primitive and object types. (It gets a bit blurred with strings, but you can pretend that primitive strings in JavaScript act just like numbers and such, the engine hides some details from you.)

This is an important distinction, because JavaScript is a purely pass by value language, so when you call a function and pass arguments into it, the values, not the variables, are passed. Consider:

function foo(index, a) {
    return a[index];
}

The foo function accepts an index and an array, and returns the value of the element in the array at that index.

x = foo(n, myArray);

That calls the foo function and passes in the value of n (2) and the value of myArray (a reference to an array). And so index receives the same value n has (2), and a receives the same value myArray has (a reference to an array). Since the value in the case of a and myArray is a reference to an array, they both refer to the same array:

+−−−−−−−−−+
| myArray |
+−−−−−−−−−+
|   ref   |−−−−−−−+    
+−−−−−−−−−+       |    
                  |    +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
+−−−−−−−−−+       +−−−>| instance of an array       |
|    a    |       |    +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
+−−−−−−−−−+       |    | length = 0                 |
|   ref   |−−−−−−−+    +−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
+−−−−−−−−−+

+−−−−−−−−−+
|    n    |
+−−−−−−−−−+
|    2    |
+−−−−−−−−−+

+−−−−−−−−−+
|  index  |
+−−−−−−−−−+
|    2    |
+−−−−−−−−−+

The same is true of assignment statements, and for the same reason: The variable on the left of the = receives the value of the expression on the right of it.

var n2 = n;
var a2 = myArray;

n2 receives the value of n (2), and a2 receives the value of myArray (a reference to the array). If we diagrammed myArray, a2, n, and n2, the diagram would be identical to the one above for foo's arguments index and a.

Since myArray and a point to the same array (reference the same object), if they perform mutator operations on the array, the results are visible via either reference. For example, adding a new element to the array is a mutator operation, it changes the object in place:

alert(myArray.length); // 0
alert(a.length);       // 0
a.push("test");
alert(myArray.length); // 1
alert(a.length);       // 1

It's important to understand what happened there. The variables myArray and a have not been changed, but the object they refer to has. In contrast:

a = someOtherArray;

Now a is pointing off somewhere else entirely, its value is no longer the same as the value of myArray, and so it isn't referring to the same object.


Off-topic: A better way to write

var myArray = new Array();

is

var myArray = [];

The end result is the same (a new array is created and assigned to myArray), but the second one is shorter, slightly more efficient, and guarantees you'll get an array (whereas the first one could, in an unusual situation, not give you an array — if someone has shadowed the Array symbol).

Similarly, you can write

var obj = new Object();

like this

var obj = {};

...which has similar benefits for the same reasons.


every piece of data is stored in a memory. when instantiating and object the object reference is stored in a variable. when using primitive types the actual value is stored in a variable.


A variable is a piece of memory. Anything you can put data into, including functions, uses memory. Without memory, you can't run or use what you've declared before, because you would have forgotten it.

I'm inclined to think that the specifics of how much memory is used and how is implementation-specific, which is why we see the many different performance comparisons. Since V8 and Rhino are open source implementations of Javascript interpreters used in Chrome and Firefox respectively, go check them out if you want to find out the specifics.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜