2

I an new to javascript & angularjs. I am using the following code to add values to my testvalues variable in my angularjs controller, which is bound to a table in my html. Is this the correct way?

$scope.testvalues = [];
var testvalue ;

testvalue = new Object();
testvalue["name"] = "xyz";
testvalue["size"] = 1024;
$scope.testvalues.push(testvalue);

testvalue = new Object();
testvalue["name"] = "abc";
testvalue["size"] = 2048;
$scope.testvalues.push(testvalue);

Does using new every time overwrite the existing memory or does it allocates new memory and dereferences the older one?

  • I think I understand what you meant, but "dereferencing" means [something else in programming](http://stackoverflow.com/q/4955198/5743988) – 4castle Aug 02 '16 at 05:21

3 Answers3

1

Any time you use assignment, the old reference is only overwritten for that single variable. So yes, it allocates new memory, and the object you pushed will remain untouched.

The new keyword isn't necessary though for declaring an object. Since JavaScript is dynamically typed, the variable declaration itself doesn't do anything to allocate memory. It all happens on-the-fly as needed. For example, you could have used {} instead of new Object(). Just like how you can use "" instead of new String(), or [] instead of new Array(), or function(){} instead of new Function(). It's always preferable to use the syntax literals, because it's shorter, faster, and can't be tampered with by someone redefining any of the classes.

Your code could be much simpler using array and object literal syntax:

$scope.testvalues = [{
  name: "xyz",
  size: 1234
}, {
  name: "abc",
  size: 2048
}];
4castle
  • 28,713
  • 8
  • 60
  • 94
  • I am surprised by your answer. Are you saying that javascript will deallocate the memory for testvalue's first value even though there is a reference to it in the array $scope.testvalues? – Ben Dadsetan Aug 02 '16 at 05:06
  • @BenDadsetan No, my answer was confusing. I didn't mean for those statements to sound related. – 4castle Aug 02 '16 at 05:08
0

It allocates new memory and just rereferences the variable.

Because the value of testvalue is pushed onto $scope.testvalues before it gets new values, the object {name: 'xyz', size: 1024} will not be deferenced entirely and the garbage collector will not try to free its allocated memory.

The new keyword is unlikely to mean what you believe it means in javascript. It is more to get a number of Object Oriented world like behaviors than it has anything to do with memory allocation. You probably will want to check out the answer What is the 'new' keyword in JavaScript?

Community
  • 1
  • 1
Ben Dadsetan
  • 1,505
  • 11
  • 17
0

Is this the correct way?

Regarding object creation you're doing fine.

testvalue = new Object();
testvalue["name"] = "abc";
testvalue["size"] = 2048;

But you can use {} to create an object. Read more about the differences here.

testvalue = {
    name: "abc",
    size: 2048
};

If find the last one more easily read.

Does using new every time overwrite the existing memory or does it allocates new memory and dereferences the older one?

Well, that's a more tricky one. I would say what you're really asking is: Does it allow the reference to be garbage collected? And then I would say probably not, but it may depend on the garbage collector.

According to MDN regarding Reference-counting garbage collection (the naive one).

An object is considered garbage collectable if there is zero reference pointing at this object.

Then I would say that it wouldn't be garbage collected since there's still a reference to the first object. See this snippet below. There are two objects in the array, and therefore they cannot be garbage collected yet.

var testvalues = [];
var testvalue ;

testvalue = new Object();
testvalue["name"] = "xyz";
testvalue["size"] = 1024;
testvalues.push(testvalue);

testvalue = new Object();
testvalue["name"] = "abc";
testvalue["size"] = 2048;
testvalues.push(testvalue);
console.log(testvalues.length); // Will print "2".

So when you really need to free up some memory you should null the array and the object. However, in real life, this is rarely needed unless you got a really big array.

$scope.testvalues = null;
testvalue = null;
Community
  • 1
  • 1
smoksnes
  • 9,330
  • 3
  • 41
  • 57