在JavaScript中,哪种创建具有属性的对象的方法最有效? [英] In JavaScript, which method of creating an object with properties is most efficient?

查看:65
本文介绍了在JavaScript中,哪种创建具有属性的对象的方法最有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript有点新鲜。我已经在客户端使用它一段时间了,但现在我冒险进入服务器端Javascript。这个问题是关于Javascript对象,关于它们的创建和它们的属性的有效定义。

I'm a little new to JavaScript. I've used it here and there on the client-side for a while, but now I am adventuring into server-side Javascript. This question is about Javascript objects, regarding their creation and the efficient definition of their properties.

我已经看过(好几次)创建一个对象 var o = {}; 现在优先于 var o = new Object(); 出于性能原因。在向对象添加属性方面的性能是否存在差异?

I've seen (several times) that creating an object as var o = {}; is now preferred over var o = new Object(); for performance reasons. Are there differences in performance in how you add properties to an object?

例如,这种情况之间的性能是否存在差异:

For instance, is there any difference in performance between this situation:

var o = {
  thing1: "yardigooven",
  thing2: "goovenyardi",
};

这种情况?:

var o = {};
o.thing1 = "yardigooven";
o.thing2 = "goovenyardi";

我假设第一个案例是首选,但我想在写完所有案例之前确定我的对象定义就是这样。

I'm assuming the first case is preferred, but I want to make sure before I write all of my object definitions that way.

谢谢。

推荐答案

all, var o = {}; var o = new Array(); 不一样。第一个初始化一个对象,第二个初始化一个数组。 var o = {}; var o = new Object(); 等效。

First of all, var o = {}; and var o = new Array(); aren't the same. The first initializes an object, the second an array. var o = {}; and var o = new Object(); are equivalent.

现在讲述使用对象文字而不是之后添加属性的性能。哪一个最快?答案是,我们不在乎,你也不应该。 如果性能存在差异,即使您同时创建了100万个对象,它也会永远不会产生影响,这种情况不会发生。

Now about the performance of using an object literal instead of adding the properties after. Which one is fastest? The answer is, we don't care, and you shouldn't either. If there is a difference in performance, it will be so small that it will never impact, even if you create 1 million objects at once, which is unlikely to ever happen.

这称为过早优化,是许多中级程序员的祸根。除非您开始遇到性能问题,否则不要担心优化任何事情。然后使用分析器检测瓶颈在哪里并解决它。只是担心制作你的应用程序。

This is called premature-optimization, and is the bane of many intermediate programmers. Don't worry about optimizing anything unless you start having performance problems. Then you use a profiler to detect where the bottleneck is and solve it. Just worry about making your app.

为了完整起见,这里是我在jsperf上运行的测试。在我的浏览器Chrome 15中,对象文字初始化速度提高了53%。哇,53%,这是巨大的权利吗?除非你把鼠标放在初始化后使用属性的测试的工具提示上,否则你会看到类似

For completeness' sake, here is a test I ran on jsperf. In my browser, Chrome 15, the object literal initialization was 53% faster. Wow, 53%, that's huge right? Except if you put your mouse over the tooltip for the test that uses properties after initialization, you'll see it says something like


Ran 681,285时间在0.077秒。

Ran 681,285 times in 0.077 seconds.

您的数字可能会有所不同,但您将能够观察到被认为最慢的方法仍然相当快任何标准。我认为可以说两者都足够快,可以用于任何目的。只需使用您喜欢的那个。

Your numbers may differ, but you'll be able to observe that the method considered slowest still goes pretty fast by any standards. I think it's safe to say that both are fast enough for any purpose. Just use the one you prefer.

这篇关于在JavaScript中,哪种创建具有属性的对象的方法最有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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