Javascript:{} vs new Object()性能 [英] Javascript: {} vs new Object() performance

查看:90
本文介绍了Javascript:{} vs new Object()性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想理解为什么两者都做同样的事情会有什么不同?

I want to understand why is the difference in performance when both does same thing?

基准

推荐答案

{}的效果可以解释为:


  1. {}是Javascript中对象的文字,文字的评估速度更快。

  2. 作为一个额外的好处,文字在你的代码中占用更少的空间,所以整个文件大小是
    更小。

  3. 文字代码的最终结果是与新的Object()代码相同,但它几乎在所有浏览器中执行得更快(Firefox 3.5几乎没有差别)。

  4. 随着对象属性和数组项的数量增加,所以使用文字也有好处。

  1. {} is the literal for object in Javascript, and literals are evaluated faster.
  2. As an added bonus, literals take up less space in your code, so the overall file size is
    smaller.
  3. The end result of literal code is the same as the new Object() code, but it is executed faster in almost all browsers (Firefox 3.5 shows almost no difference).
  4. As the number of object properties and array items increases, so too does the benefit of using literals.

对象文字{}是执行由于Javascript中的范围管理机制,速度更快

执行JavaScript代码时,会创建执行上下文。执行上下文(有时也称为范围)定义了执行代码的环境。

When JavaScript code is being executed, an execution context is created. The execution context (also sometimes called the scope) defines the environment in which code is to be executed.

在页面加载时创建全局执行上下文,以及其他执行上下文在函数执行时创建,最终创建一个执行上下文堆栈,其中最顶层的上下文是活动的上下文。

A global execution context is created upon page load, and additional execution contexts are created as functions are executed, ultimately creating an execution context stack where the topmost context is the active one.

每个执行上下文都有一个与之关联的范围链,这是用于标识符解析。范围链包含一个或多个定义执行上下文的范围内标识符的变量对象。

Each execution context has a scope chain associated with it, which is used for identifier resolution. The scope chain contains one or more variable objects that define in-scope identifiers for the execution context.

全局执行上下文在其范围链中只有一个变量对象,并且此对象定义JavaScript中可用的所有全局变量和函数。

The global execution context has only one variable object in its scope chain, and this object defines all of the global variables and functions available in JavaScript.

创建函数(但未执行)时,将分配其内部[[Scope]]属性包含创建它的执行上下文的范围链(无法通过JavaScript访问内部属性,因此您无法直接访问此属性)。

When a function is created (but not executed), its internal [[Scope]] property is assigned to contain the scope chain of the execution context in which it was created (internal properties cannot be accessed through JavaScript, so you cannot access this property directly).

稍后,当执行流程转换为函数,创建激活对象并使用其值,参数,命名参数以及函数本地的任何变量进行初始化。激活对象首先出现在执行上下文的作用域链中,然后是函数[[Scope]]属性中包含的对象。

Later, when execution flows into a function, an activation object is created and initialized with values for this, arguments, named arguments, and any variables local to the function. The activation object appears first in the execution context’s scope chain and is followed by the objects contained in the function’s [[Scope]] property.

在代码执行期间,标识符如通过搜索执行上下文的范围链来解析变量和函数名称。

During code execution, identifiers such as variable and function names are resolved by searching the scope chain of the execution context.

标识符解析从范围链的前面开始,然后继续向后。请考虑以下代码:

Identifier resolution begins at the front of the scope chain and proceeds toward the back. Consider the following code:

function Add(n1, n2) {
  this.n1 = n1;
  this.n2 = n2;
  this.val = this.n1 + this.n2;
}

var result = new Add(5, 10);

执行此代码时,add函数的[[Scope]]属性仅包含全局变量对象。

When this code is executed, the add function has a [[Scope]] property that contains only the global variable object.

当执行流入add函数时,会创建一个新的执行上下文,并将包含this,arguments,n1和n2的激活对象放入作用域链中。

As execution flows into the add function, a new execution context is created, and an activation object containing this, arguments, n1, and n2 is placed into the scope chain.

下图中,执行上下文和范围链的关系说明了执行add函数时发生的幕后对象关系。

Below Figure , "Relationship of execution context and scope chain" illustrates the behind-the-scenes object relationships that occur while the add function is being executed.

在add函数内部,执行函数时需要解析标识符num1和num2。

Inside the add function, the identifiers num1 and num2 need to be resolved when the function is executing.

此分辨率通过检查作用域链中的每个对象来执行,直到找到特定标识符。

This resolution is performed by inspecting each object in the scope chain until the specific identifier is found.

搜索从作用域链中的第一个对象开始,该对象是包含函数局部变量的激活对象。

The search begins at the first object in the scope chain, which is the activation object containing the local variables for the function.

如果在那里找不到标识符,则检查作用域链中的下一个对象是否有标识符。找到标识符后,搜索将停止。

If the identifier isn’t found there, the next object in the scope chain is inspected for the identifier. When the identifier is found, the search stops.

对于此示例,标识符num1和num2存在于本地激活对象中,因此搜索永远不会转到全局对象。

In the case of this example, the identifiers num1 and num2 exist in the local activation object and so the search never goes on to the global object.

了解JavaScript中的范围和范围链管理非常重要,因为标识符解析性能与范围链中要搜索的对象数量直接相关。

Understanding scopes and scope chain management in JavaScript is important because identifier resolution performance is directly related to the number of objects to search in the scope chain.

范围链越远,标识符就越存在,搜索持续的时间越长,访问该变量所需的时间越长;如果范围未得到妥善管理,则会对脚本的执行时间产生负面影响。

The farther up the scope chain an identifier exists, the longer the search goes on and the longer it takes to access that variable; if scopes aren’t managed properly, they can negatively affect the execution time of your script.

这篇关于Javascript:{} vs new Object()性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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