a.b.c.d 和 a[‘b’][‘c’][‘d’],哪个性能更高(a. B.c.d and a [‘b ‘] [‘c’] [‘d ‘], which has higher performance)

应该是  比  性能高点,后者还要考虑  中是变量的情况,再者,从两种形式的结构来看,显然编译器解析前者要比后者容易些,自然也就快一点。

a.b.c.d
a['b']['c']['d']
[ ]

测试如下:

function compare(times) { let a = { key: {} }; let temp = a; for (let i = 0; i < times; i++) { let tmp = temp[‘key’]; tmp[‘key’] = {}; temp = tmp; } temp[‘key’][‘key’] = ‘surprise’;

let d0 = new Date(); let i = a; while (i[‘key’] !== ‘surprise’) { i = i[‘key’]; } console.log(‘[] time’, new Date() – d0);

let d1 = new Date(); let ii = a; while (ii.key !== ‘surprise’) { ii = ii.key; } console.log(‘. time’, new Date() – d1);}

然而:O(n)或者O(2n)时间复杂度都是n,在统计上都是一个量级,比较没有太大意义

————————

The performance of , should be higher than that of , and the latter should also consider the situation that , is a variable. Moreover, from the two forms of structure, it is obvious that the compiler is easier to parse the former than the latter, and naturally faster.

a.b.c.d
a['b']['c']['d']
[ ]

The tests are as follows:

function compare(times) { let a = { key: {} }; let temp = a; for (let i = 0; i < times; i++) { let tmp = temp[‘key’]; tmp[‘key’] = {}; temp = tmp; } temp[‘key’][‘key’] = ‘surprise’;

let d0 = new Date(); let i = a; while (i[‘key’] !== ‘surprise’) { i = i[‘key’]; } console.log(‘[] time’, new Date() – d0);

let d1 = new Date(); let ii = a; while (ii.key !== ‘surprise’) { ii = ii.key; } console.log(‘. time’, new Date() – d1);}

However, the time complexity of O (n) or O (2n) is n, which is an order of magnitude in statistics, and the comparison is not of great significance