Implementing a simple DataBase now.
The duplicate tag for this question is a bad judge, I think. The linked question does nothing for my requirement.
A user ID refer a user data, and I have 2 options to do that as follows: DB1
or DB2
,
var DB1 = {
user: {}
};
var DB2 = {
user: []
};
DB1.user['001'] = {
email: 'name@email.com',
name: 'ken'
};
DB2.user['001'] = {
email: 'name@email.com',
name: 'ken'
};
console.log(DB1.user['001']);
console.log(DB2.user['001']);
The both DB1 and DB2 behaves as expected exactly in the same way, at least it appears so to me.
Is there any difference? Is just a way to express {} or []?
If different which one is more efficient?
Thanks for your thought.
UPDATE:
Thank you very much for all who kindly give me answers in detail.
I probably understand in this case, the array [] for associative manner is actually an object.
In this case, it behaves the same (as the object not array), fine I know that.
So, what I would like to know is , in this case, is it just a difference of the expression [] and {}.
More importantly, in this case, if it's not only the expression, how different and which one is more efficient, that's what I would like to know. Thank you.
UPDATE2:
Ok, this is not that obvious as many people thought. I've wrote a test code as follows:
var DB1 = {
user:
{}
};
var DB2 = {
user: []
};
var DB3 = {
user: []
};
for (var i = 0; i < 100000; i++)
{
var id = ('0000' + i)
.slice(-5);
DB1.user[id] = {
email: 'name@email.com',
name: 'ken'
};
DB2.user[id] = {
email: 'name@email.com',
name: 'ken'
};
DB3.user[i] = {
email: 'name@email.com',
name: 'ken'
};
}
//confirm the value just in case
console.log(DB1.user['00000']);
console.log(DB1.user['00001']);
console.log(DB1.user['99999']);
console.log(DB1.user['100000']);
for (var t = 0; t < 10; t++)
{
console.log('-----------test ' + t);
console.time('DB1');
for (var i = 0; i < 100000; i++)
{
var id = ('0000' + i)
.slice(-5);
var data = DB1.user[id];
}
console.timeEnd('DB1');
console.time('DB2');
for (var i = 0; i < 100000; i++)
{
var id = ('0000' + i)
.slice(-5);
var data = DB2.user[id];
}
console.timeEnd('DB2');
console.time('DB3');
for (var i = 0; i < 100000; i++)
{
var id = ('0000' + i)
.slice(-5);
var id1 = id * 1;
var data = DB3.user[id1];
}
console.timeEnd('DB3');
}
Result:
{ email: 'name@email.com', name: 'ken' }
{ email: 'name@email.com', name: 'ken' }
{ email: 'name@email.com', name: 'ken' }
undefined
-----------test 0
DB1: 46ms
DB2: 68ms
DB3: 28ms
-----------test 1
DB1: 39ms
DB2: 33ms
DB3: 26ms
-----------test 2
DB1: 32ms
DB2: 39ms
DB3: 25ms
-----------test 3
DB1: 57ms
DB2: 33ms
DB3: 27ms
-----------test 4
DB1: 39ms
DB2: 35ms
DB3: 27ms
-----------test 5
DB1: 39ms
DB2: 32ms
DB3: 27ms
-----------test 6
DB1: 33ms
DB2: 36ms
DB3: 26ms
-----------test 7
DB1: 39ms
DB2: 41ms
DB3: 40ms
-----------test 8
DB1: 32ms
DB2: 32ms
DB3: 28ms
-----------test 9
DB1: 36ms
DB2: 31ms
DB3: 28ms
For 100000 records, the array DB is slightly faster, but I think I will use Associative array style Object DB.