简体   繁体   中英

size of object keys in javascript

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM