Use Maps instead of Objects for better performance

preview_player
Показать описание

#javascript #performance #webdev #webperformance
Рекомендации по теме
Комментарии
Автор

what if you are just adding keys, will the performance still be better?

anonymousBl
Автор

Object.create(null) will make a completely empty object.

PauloSantosk
Автор

Just a quick note: A substantial disadvantage for Maps is that they are not supported with JSON directly.

vicitacious
Автор

But how you can load maps from json file or rest api response?

TarekFaham
Автор

Note: objects are perfectly fine. Map() is half baked and needs some extra TLC in the API.

for...in doesn't work for Maps and wont error out if used. for...of is an alternative.

clarkhinchcliff
Автор

Interesting. I ran your benchmark on Firefox and Chrome on my device, with opposite results. Firefox is thrice as fast with objects than maps. And Chrome is thrice as fast with Maps than objects

petrlaskevic
Автор

Objects are faster in many ways, it all depends on the specific use case and whether you actually need this performance :)

aleksander