Could somebody explain, why Map
is slower, than Object
in the code below?
Docs says, that Map
is optimized for performance, but tests shows poor results.
#!/usr/bin/env node
const object = {},
map = new Map(),
prefix = "a",
iteratiions = 100_000;
for ( let n = 0; n < 100_000; n++ ) {
object[prefix + n] = n;
map.set( prefix + n, n );
}
const t1 = Date.now();
for ( let n = 0; n < iteratiions; n++ ) {
map.delete( prefix );
map.set( prefix, 1 );
}
console.log( "Map:", Date.now() - t1, "ms" );
const t0 = Date.now();
for ( let n = 0; n < iteratiions; n++ ) {
delete object[prefix];
object[prefix] = 1;
}
console.log( "object:", Date.now() - t0, "ms" );
Output:
Map: 7558 ms
object: 11 ms