Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么定义带有或不带引号的JavaScript对象文字有区别?_Javascript_Performance - Fatal编程技术网

为什么定义带有或不带引号的JavaScript对象文字有区别?

为什么定义带有或不带引号的JavaScript对象文字有区别?,javascript,performance,Javascript,Performance,在纯JavaScript中,建议以下两个代码段等效: // Snippet one var myObject = { "test":"test" } // Snippet two var myObject = { test:"test" } 但是,规范要求使用引号 定义对象文字时使用引号(如果有的话)何时正确?这对口译员有什么影响吗 我编写了一个测试函数,它使用performance.now()()来测量创建一百万个简单对象所需的时间: function test(iteration

在纯JavaScript中,建议以下两个代码段等效:

// Snippet one
var myObject = {
  "test":"test"
}

// Snippet two
var myObject = {
  test:"test"
}
但是,规范要求使用引号

定义对象文字时使用引号(如果有的话)何时正确?这对口译员有什么影响吗

我编写了一个测试函数,它使用
performance.now()
()来测量创建一百万个简单对象所需的时间:

function test(iterations) {
  var withQuotes = [];
  var withoutQuotes = [];

  function testQuotes() {
      var objects = [];
      var startTime, endTime, elapsedTimeWithQuotes, elapsedTimeWithoutQuotes;

      // With quotes
      startTime = window.performance.now();

      for (var i = 0; i < 1000000; i++) {
          objects[objects.length] = {
              "test": "test"
          };
      }

      endTime = window.performance.now();
      elapsedTimeWithQuotes = endTime - startTime;

      // reset
      objects = undefined;
      startTime = undefined;
      endTime = undefined;
      objects = [];

      // Without quotes
      startTime = window.performance.now();

      for (var i = 0; i < 1000000; i++) {
          objects[objects.length] = {
              test: "test"
          };
      }

      endTime = window.performance.now();
      elapsedTimeWithoutQuotes = endTime - startTime;

      return {
          withQuotes: elapsedTimeWithQuotes,
          withoutQuotes: elapsedTimeWithoutQuotes
      };
    }

  for (var y = 0; y < iterations; y++) {
      var result = testQuotes();
      withQuotes[withQuotes.length] = result.withQuotes;
      withoutQuotes[withoutQuotes.length] = result.withoutQuotes;

      console.log("Iteration ", y);
      console.log("With quotes: ", result.withQuotes);
      console.log("Without quotes: ", result.withoutQuotes);
  }

  console.log("\n\n==========================\n\n");
  console.log("With quotes average: ", (eval(withQuotes.join("+")) / withQuotes.length));
  console.log("Without quotes average: ", (eval(withoutQuotes.join("+")) / withoutQuotes.length));
}

test(300);
功能测试(迭代){
var with Quotes=[];
不带引号的var=[];
函数testQuotes(){
var对象=[];
var startTime、endTime、elapsedtime with quotes、elapsedtime without quotes;
//引用
startTime=window.performance.now();
对于(变量i=0;i<1000000;i++){
对象[objects.length]={
“测试”:“测试”
};
}
endTime=window.performance.now();
elapsedTimeWithQuotes=结束时间-开始时间;
//重置
对象=未定义;
开始时间=未定义;
endTime=未定义;
对象=[];
//无报价
startTime=window.performance.now();
对于(变量i=0;i<1000000;i++){
对象[objects.length]={
测试:“测试”
};
}
endTime=window.performance.now();
elapsedTimeWithoutQuotes=结束时间-开始时间;
返回{
withQuotes:elapsedTimeWithQuotes,
不带引号:ElapsedTime不带引号
};
}
对于(变量y=0;y
我得到的结果意味着使用引号(稍微)要快一些。为什么会这样

在我的浏览器上,我从测试函数中获得这些结果(平均超过300次迭代):

带引号:167.6750966666926ms
不带引号:187.5536800000494ms

当然,我的测试功能也很可能是无用的

属性名称可以是任何字符串,包括空字符串。这个 在以下情况下,对象文字中围绕属性名称的引号是可选的: 该名称将是合法的JavaScript名称,而不是保留字。所以 “first name”周围需要引号,但在“first name”周围是可选的 名字


来源:Douglas Crockford的“JavaScript:好的部分”。

如果你看单个结果,它们是完全随机的,所以是的,它是一个不完整的测试函数