1

I am building a number of HTML5 games and I am not sure about one thing in javascript.

When dealing with a large object (with a lot of attributes and methods), is it somehow different, if I store the attribute value in a variable?

Say I have to check for some value in application.data.setings.foo.bar multiple times per second. Should I store it in a variable fooBar? If I understand it correctly, the variable would be just a reference, so it shouldn't matter.

so: Should you store values of large objects' attributes in variables?

4

3 回答 3

1

我相信你要问的是:

如果您有一个名为的变量:

application.data.settings.foo.bar = { some: 'value' }

通过使用application.data.settings.foo.bar- 或改为说:var bar = application.data.settings.foo.bar然后引用bar.

我的猜测是,对变量的赋值可能会稍微提高性能 - 仅仅是因为解释器为了访问相关项目而采取的步骤更少。如果您使用application.data.settings.foo.bar- 解释器必须利用对application对象的引用,然后引用它的data属性,然后引用它的setting属性,然后引用它的foo属性,然后引用它的bar属性。这是5个步骤。

如果您将它引用到一个局部变量中进​​行访问 - 您仍然会点击同一个对象,但您会直接在每个引用上点击它。

然而,归根结底,这不太可能是非常显着的性能增强,除非您正在执行大量繁重的快速访问循环或类似的事情。

于 2012-09-20T17:26:13.737 回答
-1

对于数字,如果将其放在变量中,您可能会看到性能提升

于 2012-09-20T17:23:07.517 回答