简体   繁体   English

javascript引擎是否执行多维数组的任何优化?

[英]Do javascript engines perform any optimization of multidimentional arrays?

I know that modern javascript engines create hidden classes behind the scenes, so it's not recommended to change objects at runtime (if you have a large set of objects with same structure). 我知道现代javascript引擎在幕后创建隐藏类 ,因此不建议在运行时更改对象(如果你有大量具有相同结构的对象)。

Has anyone heard about internal optimizations of two-dimentional arrays? 有没有人听说过二维数组的内部优化?

I create a two-dimentional array this way: 我用这种方式创建了一个二维数组:

var map = [];
for (i = 0; i < 100000; i++) {
    map.push(Array(2));
} 

I'm planning to break the nice structure like this: 我打算打破这样漂亮的结构:

map[100].push(/* third element! */)

Will this have a dramatic impact on performance? 这会对性能产生巨大影响吗?

V8 doesn't do any optimizations for multi-dimensional arrays, so there should be no measurable impact on performance there. V8不对多维数组进行任何优化,因此对性能不应产生可测量的影响。

I can't speak for other JavaScript engines, but would find it surprising if the pattern you describe had negative impact anywhere. 我不能代替其他JavaScript引擎,但如果您描述的模式在任何地方都有负面影响,我会发现它令人惊讶。 The kind of optimization you're talking about typically requires making assumptions and guarding them with checks. 你所谈论的那种优化通常需要做出假设并用支票来保护它们。 Given the highly dynamic nature of JavaScript arrays, this is brittle enough for one-dimensional arrays; 鉴于JavaScript数组的高度动态性,这对于一维数组来说是脆弱的; checking any non-trivial property of two-dimensional arrays is probably just not worth the cost. 检查二维数组的任何非平凡属性可能不值得花费。

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

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