, , , , , , , ?
, , .
x = {};
y = new Set();
z = [];
console.time('Insert into Object');
for (var i = 0; i < 1000000; i++) {
x[i] = 0;
}
console.timeEnd('Insert into Object');
console.time('Insert into Set');
for (i = 0; i < 1000000; i++) {
y.add(i);
}
console.timeEnd('Insert into Set');
console.time('Insert into Array');
for (i = 0; i < 1000000; i++) {
z[i] = 0;
}
console.timeEnd('Insert into Array');
var t = 0;
console.time('Retrieve from Object');
for (i = 0; i < 1000000; i++) {
t = x[i];
}
console.timeEnd('Retrieve from Object');
console.time('Retrieve from Set');
for (i = 0; i < 1000000; i++) {
t = y.has(i);
}
console.timeEnd('Retrieve from Set');
console.time('Retrieve from Array');
for (i = 0; i < 1000000; i++) {
t = z[i];
}
console.timeEnd('Retrieve from Array');
console.log(t);
, , O (1) O (n) ( n - ), , . , javascript -.
, : ( )
( )
Insert into Object: 67.558ms
Insert into Set: 259.841ms
Insert into Array: 64.297ms
Retrieve from Object: 19.337ms
Retrieve from Set: 149.968ms
Retrieve from Array: 3.981ms
, ...
:
Insert into Object: 19.103ms
Insert into Set: 40.645ms
Insert into Array: 16.384ms
Retrieve from Object: 40.116ms
Retrieve from Set: 30.672ms
Retrieve from Array: 70.050ms
. -, - . -, , ( , ).
, , , ( ). . , , , , . . , .