使用Object.create(null)创建空对象的影响和好处 [英] Impacts and benefits of creating empty object using Object.create(null)

查看:446
本文介绍了使用Object.create(null)创建空对象的影响和好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我做了一个快速的jsperf测试用例,以显示显而易见的:
Object.create(null)比使用{}语法创建对象要慢得多.
http://jsperf.com/js-object-creation-null-proto

但是考虑到这一事实,前一种情况有时可以成为涉及优化和性能的好选择吗?换句话说,处理最轻量级的js对象是否可以充分提高性能,以使其在某些情况下成为使用它的合理选择.

我指的是您要大量访问对象属性或大量使用for in循环的情况.

此方法在库中使用的风险太大吗?在库中,人们可能会被带走这些不具有标准Object原型带来的标准属性的对象?

另外,您知道创建最轻量级js对象的另一种方法吗?

PS:我做了一个简短的测试用例,以显示使用Object.create(null)并访问属性比使用{}更快的情况.
http://jsperf.com/js-object-creation-and-accessing-properties

我认为用Object.create(null)创建对象通常是不必要的,不值得您牺牲浏览器的兼容性和清晰度.使用{}语法创建对象时,实际上是在做Object.create(Object.prototype),这将创建一个可以访问Object.prototype中的属性的对象.这意味着您可以直接从对象访问toStringhasOwnProperty之类的方法,但是如果需要,也可以覆盖它们-尽管在可能的情况下应避免这样做.至于性能,差异非常很小,对象上只有两个属性(如jsperf所示).如果您的对象具有数百或数千个属性,则几乎不会存在差异.

总而言之,如果您不希望自己的代码表现得更好,则应始终力求清晰,并尝试改进代码的高级算法设计(不是您尚未做到的);并且仅在绝对必要时才担心微优化.

First of all, I made a quick jsperf test case to show the obvious :
Object.create(null) is way slower than creating a object with the {} syntax.
http://jsperf.com/js-object-creation-null-proto

But given this fact, can the former case be a good alternative sometimes concerning optimization and performance? In other words, does manipulating the most lightweight js object can increase performance enough so that it became a reasonable choice to use it in some cases.

I am refering to cases where you are going to access the object properties a lot, or making an heavy use of the for in loop for example.

Is this method too much risky to be used in a library where people could be lead to heandle these object that doesn't own the standard properties bring by the standard Object prototype?

Also, do you know another (quicker) way to create the most lightweight js object?

PS : I made a brief test case to show a case where using a Object.create(null) and accessing properties is a little more quickier than with {}.
http://jsperf.com/js-object-creation-and-accessing-properties

解决方案

I think creating objects with Object.create(null) generally unnecessary and not worth the browser compatibility and clarity you sacrifice. When you create an object with the {} syntax you are essentially doing Object.create(Object.prototype), which creates an object with access to the properties in Object.prototype. This means that you have access to methods like toString and hasOwnProperty directly from the object, but can also override them if you want — although you should avoid this if possible. As for performance, the difference is very small with only two properties on the object (as demonstrated on jsperf). If your object had hundreds or thousands of properties the difference would be almost nonexistent.

In conclusion, if you wan't your code to perform better you should always strive for clarity and try to improve the high level, algorithmic design of your code (not that you haven't already); and only worry about micro-optimizations only when absolutely necessary.

这篇关于使用Object.create(null)创建空对象的影响和好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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