使用临时变量代替重复的完整引用是否更好? [英] Is it better to use temporary variables in place of repeated full references?

查看:52
本文介绍了使用临时变量代替重复的完整引用是否更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个类似的对象:

Say we have an object something like:

var foo = { a: { b: { c: { d: { e: { f: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] } } } } } };

我们希望在函数中使用对象的各个部分:

And we want to use the various parts of the object in functions along the lines of:

function doStuff() {
    if ( foo.a.b.c.d.e.f[ 5 ] >= some_important_filter_value ) {
        alert( "Some important filter value is " + foo.a.b.c.d.e.f[ 5 ] +
            "!!\nRun for your lives!!" );
    }
}

为了论证,foo 中包含的值不会在函数执行过程中发生变化,或者就此而言,根本不会发生变化.

And for argument's sake, the values contained within foo aren't going to be changing during the course of the function execution, or for that matter, very often at all.

更好从对象中读取值并将它们存储为临时变量/常量,还是完全可以使用通过对象的完整路径foo 作为价值的参考?

Is it better to read the values from the object and store them as temporary variables/constants, or perfectly fine to use the full path through the object foo as the reference for the value?

function doStuff() {
    var mem = foo.a.b.c.d.e.f[ 5 ];
    if ( mem >= some_important_filter_value ) {
        alert( "Some important filter value is " + mem +
            "!!\nRun for your lives!!" );
    }
}

更好",如果是这样,具体是什么方式?

"better", and if so, in exactly what way(s)?

我自己在谷歌浏览器的 JS 控制台中进行的有限测试显示 100000 while 循环深度查询一个对象,与查询临时变量的循环次数相同,大约在同样的速度.

My own limited testing in Google Chrome's JS console shows 100000 while loops deeply querying an object, against the same number of loops querying a temporary variable to run at approximately the same speed.

有效地将 foo 中的值复制并存储到临时变量中以供多次使用,在某些情况下可能具有实用价值,因为可以减少代码/文件大小和可读性可能会改进,但是为了这个问题的目的,把它放在一边,还有什么其他的论点可以支持它?

Effectively copying and storing a value from foo into a temporary variable for use any number of times, may in certain circumstances have a practical value insofar that code/file size may be reduced and readability may be improved, but leaving that aside for the purposes of this question, what other arguments in its favour can be made?

对于现代 JS 解释器来说,创建和处理临时变量肯定比直接引用对象中的值更重要吗?

The creation and handling of a temporary var is surely less trivial for modern JS interpreters than simply referencing the value within the object directly?

为什么以及如何var temp = foo.abc..更好比跳过定义并仅通过我们已有的引用使用值?

Why and how would var temp = foo.a.b.c... be better than skipping the definition and just using the value via the reference we already have?

我想从根本上说,我要问的是 - 我的参考中有多少个点真的很重要,因为从长远来看,它只是一个参考 - 或者是吗?

I suppose that fundamentally what I'm asking is - does it really matter how many dots there are in my reference, since in the long run, it's just a reference - or is it?

推荐答案

在迂腐的奇妙领域,您将一次又一次地为解决这些引用付出代价.

In the wonderful realm of pedantry, you are going to pay the penalty of resolving those references, time and again.

如果这些是类"(构造对象,可能会引用 proto 链上的值),那么您将付出额外的代价,因为无法在对象上找到值,并提升proto 链,直到你这样做(或者点击 Object.prototype,仍然错过这个属性,并抛出一个引用错误,当你尝试访问那个丢失对象的一个​​属性时).

If these were "classes" (constructed objects, which might be referencing values on the proto chain), then you pay the added penalty of failing to find the value on the object, and ascending the proto chain until you do (or hit Object.prototype, still miss the property, and throw a reference error, when you try to access a property of that missing object).

现在,总而言之,如果在现代引擎的压力测试中增加 1 毫秒,我会大吃一惊.

Now, all told, if that added even 1ms in your stress tests of modern engines, I would be flabbergasted.

Chrome 的 V8 将大大有助于即时重新编译代码,其中包括展开循环、内联函数等;只要您每次都始终如一地使用函数(传递类似的参数/参数类型).

Chrome's V8 will go a long way to recompile code on the fly, which includes things like unwinding loops, inlining functions, et cetera; so long as you use functions consistently (passing similar arguments/argument-types), every time.

我相信,大约在 2012 年左右,有一些关于在 Chrome 中编写游戏引擎的讨论很好.

There were some good talks on this stuff, relating to writing game engines in Chrome, circa 2012, I believe.

道德是你所做的大部分事情,如果做超过几次,只会被编译成一个内联操作......

The moral is that most of what you do, if done more than a couple of times, will just be compiled down to an inline operation...

...在新浏览器中...

...in a new browser...

当你做了类似的事情时,你会受到打击

You would take a performance hit the moment you did something like

if (i % 3001) { a.b.e.f.g[6]; }
else { a.b.c.d.e.f[5]; }

...再说一遍,这是现代引擎.

...again, that's modern engines.

如果您要在运行 IE6 的 2003 Windows XP 系统上运行负载测试,由于上述原因,您的数字应该不同.

If you were to run your load test on a 2003 Windows XP system, running IE6, your numbers should be different, due to the aforementioned reasons.

他们不应该关闭数百毫秒...
然而,它们应该反映多次取消引用值的性能影响,如果属性是 proto 链接的,则性能损失应该增加,并根据解析每个属性的链上的距离再次增加.

They shouldn't be hundreds of ms off...
They should, however, reflect the performance hit of dereferencing values multiple times, and that performance penalty should increase if the properties are proto linked, and increase again based on the distance up the chain to resolve each property.

也就是说,我指的是 abca["b"]["c"] 的测试明显不同的浏览器.

To wit, I'm referring to the browsers where tests of a.b.c versus a["b"]["c"] were perceivably different.

这篇关于使用临时变量代替重复的完整引用是否更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆