Reputation: 542
I have a simple question about javascript.
Does it matter (as far as performance is concerned) the size of the keys in a javascript object ?
Here are 2 examples :
a. short keys
var obj = {};
obj['a'] = { x : 1, y : 0 };
obj['b'] = { x : 1, y : 0 };
obj['c'] = { x : 1, y : 0 };
b. long keys
var obj = {};
obj['this_is_a_long_key'] = { x : 1, y : 0 };
obj['this_is_a_longer_key'] = { x : 1, y : 0 };
obj['this_is_the_longest_key'] = { x : 1, y : 0 };
So if I am searching/editing very often the properties ( which can be many ) of those objects, does the size of the key have to do anything with the performance of the searching/editing etc actions ?
thanks
Upvotes: 1
Views: 1461
Reputation: 413757
Large keys aren't free, because ultimately the runtime has to perform hash and comparison operations when looking up properties. However, unless you've got a very large number of long keys, it's probably not worth worrying about.
Modern JavaScript environments are generally optimized enough to allow you to concentrate on writing code the way you want, so that (as Joe Armstrong famously says about Erlang) your code is as beautiful as it can be. Concentrate on algorithmic efficiency, and worry about details like this if you're really up against a performance barrier.
Upvotes: 4