You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JefferyHo, neofanfei, dolphinlin, Lionad-Morotar, RuoWeng123 and 88 moreliyanana and pallyoungliyanana, lennonzhong, wodeMAX and realwdsrealwdsliyanana and realwdsdaiyunchao, JOYINF1189 and yinyihuirealwds, JOYINF1189 and zhangyemengren
The results seem to be about the same. What causes slowdows is when the contents of the bracket are a variable - at that point the compiler can no longer say to itself "Oh, ['foo'] is the same as .foo.. continuing on!".
Activity
wingmeng commentedon Apr 29, 2019
应该是

a.b.c.d
比a['b']['c']['d']
性能高点,后者还要考虑[ ]
中是变量的情况,再者,从两种形式的结构来看,显然编译器解析前者要比后者容易些,自然也就快一点。下图是两者的 AST 对比:
susiwen8 commentedon Apr 29, 2019
https://jsperf.com/dot-notation-vs-square-bracket-notation
实验证明dot notation更快,但深层原理不是太明白
LiuMengzhou commentedon Apr 29, 2019
看了楼顶的链接,知道了AST,还是很开心的。

菜🐔只会这么测试了。感觉[]是常量,性能差别很小。dot稍快
LiuMengzhou commentedon Apr 29, 2019
Zephylaci commentedon May 6, 2019
看到上面的模拟结果结果...
感觉不是很贴近实际的题目...
于是自己也写了一个...
发现如果每组测试运行多次的话,与其说性能差距小到可以忽略不记...
不如说这个差距可以算在误差范围内..
有的时候['a']['b']['c']['d']还会快一些,所以说它性能差可能从模拟的角度有些站不住脚
不知道底层是否有对路径的缓存..
如果想验证这一点的话应该考虑是否是查找的同一个对象对这个的影响..
附上我跑的结果
songguoguo927 commentedon Aug 19, 2019
var obj = {
a:{
b:{
c:{
d:1
}
}
}
}
console.time()
console.log(obj.a.b.c.d)
console.timeEnd() //default:0.763ms 0.684
// console.time()
// console.log(obj["a"]["b"]["c"]["d"])
// console.timeEnd() //default:1.100ms 0.964ms
// 结果:虽然时间都会变动,在但总体感觉使用点访问的时间要少于以[]访问。
yayxs commentedon Apr 26, 2020
对于常见编译型语言(例如:Java)来说,编译步骤分为:词法分析->语法分析->语义检查->代码优化和字节码生成。
对于解释型语言(例如 JavaScript)来说,通过词法分析 -> 语法分析 -> 语法树,就可以开始解释执行了。
参考阅读
soraly commentedon Jun 24, 2020
a.b.c.d 和 a['b']['c']['d'],哪个性能更高?
a.b.c.d 比 a['b']['c']['d'] 性能高点,因为[ ]里面有可能是字符串,有可能是变量,至少多一次判断,而a.b.c.d是直接取用该字符串当作属性名的
libin1991 commentedon Aug 27, 2020
大部分情况.快,不过对JS的v引擎来说配合JIT优化,总体来说不相上下
zizifn commentedon Dec 11, 2021
这种无论怎么样,时间都是一个量级吧,O(n)或者O(2n)时间复杂度都是n啊。在统计上都是一个量级,比较没有太大意义。