JavaScript性能:多个变量还是一个对象? [英] JavaScript Performance: Multiple variables or one object?

查看:98
本文介绍了JavaScript性能:多个变量还是一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是一个简单的性能问题,帮助我理解javascript引擎。
为此我想知道,更快的是:为某些值声明多个变量或使用包含多个值的一个对象。

this is just a simple performance question, helping me understand the javascript engine. for this I'm was wondering, what is faster: declaring multiple variables for certain values or using one object containing multiple values.

示例:

var x = 15;
var y = 300;

vs。

var sizes = { x: 15, y: 300 };

这只是一个非常简单的例子,当然可以在实际项目中有所不同。
甚至可以做到这一点吗?

this is just a very simple example, could of course differ in a real project. does this even matter?

推荐答案

这个问题的完整答案真的很长。所以我会尝试解释一些事情。首先,也许是最重要的事实,即使您使用 var 声明变量,它也取决于您执行此操作的位置。在全局范围内,您隐式地也会在对象中编写该变量,大多数浏览器将其称为 window 。例如,

A complete answer for that question would be really long. So I'll try to explain a few things only. First, maybe most important fact, even if you declare a variable with var, it depends where you do that. In a global scope, you implicitly would also write that variable in an object, most browsers call it window. So for instance

// global scope
var x = 15;

console.log( window.x ); // 15

如果我们在函数的上下文中做同样的事情就会发生变化。在函数的上下文中,我们将该变量名称写入其称为激活对象的名称中。也就是说,js引擎为你处理的内部对象。所有形式参数,函数声明和变量都存储在那里。

If we do the same thing within the context of a function things change. Within the context of a function, we would write that variable name into its such called 'Activation Object'. That is, an internal object which the js engine handles for you. All formal parameters, function declarations and variables are stored there.

现在回答你的实际问题:在一个函数的上下文中,它始终是最快的访问权限,用 var 。如果我们处于全球背景下,这又不是真的。全局对象非常庞大,访问内部的任何内容都不是很快。

Now to answer your actual question: Within the context of a function, its always the fastest possible access to have variables declared with var. This again is not necesarrily true if we are in the global context. The global object is very huge and its not really fast to access anything within.

如果我们将对象存储在对象中,它仍然非常快,但不如变量快由 var 声明。特别是访问时间确实增加了。但是,我们在这里谈论微观和纳秒(在现代浏览器实现中)。 Old'ish浏览器,特别是IE6 + 7在访问对象属性时会有巨大的性能损失。

If we store things within an object, its still very fast, but not as fast as variables declared by var. Especially the access times do increase. But nonetheless, we are talking about micro and nanoseconds here (in modern browser implementations). Old'ish browsers, especially IE6+7 have huge performance penalties when accessing object properties.

如果你真的对这样的东西感兴趣,我会推荐这本书'<强大的>高性能Javascript '作者:Nicholas C. Zakas。他测量了许多不同的技术来为您访问和存储ECMAscript中的数据。

If you are really interested in stuff like this, I highyl recommend the book 'High Performance Javascript' by Nicholas C. Zakas. He measured lots of different techniques to access and store data in ECMAscript for you.

同样,对象查找和 var <声明的变量的性能差异/ code>在现代浏览器中几乎无法测量。像FF3或IE6这样的Old'ish浏览器确实显示了对象查找/访问的基本缓慢性能。

Again, performance differences for object lookups and variables declared by var is almost not measureable in modern browsers. Old'ish Browsers like FF3 or IE6 do show a fundamental slow performance for object lookups/access.

这篇关于JavaScript性能:多个变量还是一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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