通过变量或硬编码字符串访问关联数组对象的性能

时间:2018-01-09 19:43:15

标签: javascript arrays performance

我的一个"只是好奇"这里的问题......我想知道,从性能的角度来看,你是否知道访问一个关联数组对象特定元素与更长的硬编码字符串(如更常见的做法)缩短是否有任何实质性的区别我还有那个名字字符串变量?

以循环元素时发生的情况为例:

myAAO = { entry-name1: value, entry-name2: value, long-entryX-name: value, entry-nameN: value, ... }; // my associative array object that may contain a lot of entries and may or may not contain the 'long-entryX-name' one
for (let i in myAAO) {
  if (i != 'long-entryX-name') {
    //do same things with all others myAAO[i] entries that aren't 'long-entryX-name' one
  } else {
    // I need to do specific things with this entry that are different than in all the other ones and I know i == 'long-entryX-name' already so will I have any good reason to retrieve the value doing:
    myAAO['long-entryX-name'];
    // over the usual
    myAAO[i];
    // or there wouldn't be any significant difference to prefer the former?
// I know the latter is less prone to misspelling the name for sure and I'd go with it...
  }
}

0 个答案:

没有答案