var keys = [7925181,"68113227"];
var vals = {"7925181":["68113227"],"68113227":["7925181"]};
var temp = [];
for (var i = 0; i < keys.length; i++) {
temp[keys[i]] = vals[keys[i]];
}
//alert(JSON.stringify(vals).length);
alert(JSON.stringify(temp).length);
When I run that script in Chrome I get, after a good amount of time, an output of 340666156.
My question is... how?
The mented out alert outputs 47. Seems to me that the second alert should yield the same result? That temp should pretty much be an exact copy of val?
The jsfiddle of it:
/
Oh - and if you want to crash your browser window (well it crashed my Google Chrome window) just add the following to the end:
temp.forEach(function(entry) {
alert(temp);
});
Any ideas?
var keys = [7925181,"68113227"];
var vals = {"7925181":["68113227"],"68113227":["7925181"]};
var temp = [];
for (var i = 0; i < keys.length; i++) {
temp[keys[i]] = vals[keys[i]];
}
//alert(JSON.stringify(vals).length);
alert(JSON.stringify(temp).length);
When I run that script in Chrome I get, after a good amount of time, an output of 340666156.
My question is... how?
The mented out alert outputs 47. Seems to me that the second alert should yield the same result? That temp should pretty much be an exact copy of val?
The jsfiddle of it:
http://jsfiddle/vMMd2/
Oh - and if you want to crash your browser window (well it crashed my Google Chrome window) just add the following to the end:
temp.forEach(function(entry) {
alert(temp);
});
Any ideas?
Share Improve this question asked Mar 19, 2013 at 3:13 neubertneubert 16.8k26 gold badges135 silver badges252 bronze badges 2-
1
temp.length
would be 68113227... – Passerby Commented Mar 19, 2013 at 3:16 - @Passerby—length is always at least one higher than the highest index, so 681132271 + 1 = 68113228. – RobG Commented Mar 19, 2013 at 4:00
2 Answers
Reset to default 11var keys = [7925181,"68113227"];
var vals = {"7925181":["68113227"],"68113227":["7925181"]};
var temp = {}; // <--- !!!
for (var i = 0; i < keys.length; i++) {
temp[keys[i]] = vals[keys[i]];
}
//alert(JSON.stringify(vals).length);
alert(JSON.stringify(temp).length);
http://jsfiddle/zerkms/vMMd2/2/
You're creating a sparse array, and presumably V8 initializes all the gaps with some garbage null undefined values (thanks to nnnnnn for checking that). It takes some time
@zerkms, is right. But I also wanted to pointed out why this is happening.
> var temp = [];
> temp[10] = 'test';
> temp
[ , , , , , , , , , , 'test' ]
As you can see, it creates 9 undefined values. I ran the above with nodejs so the null values are not showing up.
If I did JSON.stringfy() then you would see:
> JSON.stringify(temp)
'[null,null,null,null,null,null,null,null,null,null,"test"]'
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744326666a4568681.html
评论列表(0条)