Javascript how "length" is implemented
You know that in Javascript you can access the text / array length with the length property:
var obj = ["Robert", "Smith", "John", "Mary", "Susan"];
// obj.length returns 5;
I want to know how this is implemented. Is Javascript calculating the length property when it is called? Or is it just a static property that changes whenever the array changes. My question is being asked due to the following confusion about best practices with javascript:
for(var i = 0; i < obj.length; i++)
{
}
My problem: If it is a static property, accessing the length property on each iteration doesn't matter, but if it is calculated for each iteration, then it costs a little memory.
I have read the following definition given by ECMAScript, but it does not provide any idea of how it is implemented. I'm afraid this might give a whole array instance with length property computed at runtime, that if it is true then the above for()
is memory hazardous and the following should be used instead:
var count = obj.length;
for(var i = 0; i < count; i++)
{
}
source to share
An array in JavaScript is not a real array type, but it is a real object type.
[]. The length is not recalculated every time, it is controlled by operators ++
or --
.
Below is an example that behaves the same as a property array.length
.
var ArrayLike = {
length: 0,
push: function(val){
this[this.length] = val;
this.length++;
},
pop: function(){
delete this[this.length-1];
this.length--;
},
display: function(){
for(var i = 0; i < this.length; i++){
console.log(this[i]);
}
}
}
// output
ArrayLike.length // length == 0
ArrayLike.push('value1') // length == 1
ArrayLike.push('value2') // length == 2
ArrayLike.push('value3') // length == 3
ArrayLike.pop() // length == 2
ArrayLike.length === 2 // true
source to share
var a = ["abc","def"];
a["pqr"] = "hello";
What is a.length?
2
Why?
a.length is only updated when the array index is a numeric value. When you write
var a = ["abc","def"];
It is internally stored as:
a["0"] = "abc"
a["1"] = "def"
Note that indices are really keys, which are strings.
A few examples:
1).
var a = ["abc","def"];
a["1001"] = "hello";
What is a.length?
1002
2.) Ok, try again:
var a = ["abc","def"];
a[1001] = "hello";
What is a.length?
1002
Note. The inner array is saved as
a["0"] = "abc"
a["1"] = "def"
a["1001"] = "hello"
3.) Ok, last one:
var a = ["abc"];
a["0"] = "hello";
What is [0]?
"hello"
What is a.length?
1
It's good to know what a.length actually means: Well, now you know: a.length is more than the last numeric key present in the array.
source to share
I want to know how this is implemented. Is Javascript calculating the length property when it is called? Or is it just a static property that changes when the array changes.
Actually, your question cannot be answered at all, because all ECMA specifications say the following:
The length property of this Array object is a data property, the value is always numerically greater than the name of each property to remove whose name is an array index.
In other words, specifications define a property's invariance condition length
, but not its implementation. This means that different JavaScript engines can, in principle, implement different behavior.
source to share