简体   繁体   English

JavaScript中对象键的大小

[英]size of object keys in javascript

I have a simple question about javascript. 我有一个关于javascript的简单问题。

Does it matter (as far as performance is concerned) the size of the keys in a javascript object ? javascript对象中键的大小是否重要(就性能而言)?

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. 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. 通常,现代JavaScript环境已经过充分优化,可以让您专注于以自己想要的方式编写代码,从而(如Joe Armstrong关于Erlang的著名说法),您的代码将尽可能地优美。 Concentrate on algorithmic efficiency, and worry about details like this if you're really up against a performance barrier. 如果您真的遇到性能障碍,请专注于算法效率,并担心像这样的细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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