使用带有函数和mixins的对象而不是原型有几个性能损失? [英] Are there several performance penalties for using Objects with functions and mixins, rather than prototypes?

查看:106
本文介绍了使用带有函数和mixins的对象而不是原型有几个性能损失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中执行OOP的一种常见方法是使用附加了函数的对象,而不是使用内置原型,构造函数和 new 运算符。 Mixins通常用于通过粘贴成员变量来组合2个对象,模拟继承。

One common way to do OOP in JavaScript involves using objects with functions attached to them, instead of using the built-in prototypes, constructors and the new operator. Mixins are often used to combine 2 objects by pasting member-variables, simulating inheritance.

衡量这种做法是否会对性能产生影响很难,因为特定的基准测试不应该在这种情况下说多少。是否存在此类性能损失,以及它们有多严重?

Measuring whether that kind of practice has consequences for performance is hard, because specific benchmarks don't tell much in this case. Are there such performance penalties, and how severe are them?

推荐答案

如果使用构造函数,原型进行OOP, new s等,这是V8(Chrome和Node.js中的JS引擎)特别优化的东西。您的用户属性甚至会与其内部属性并排存储,例如在C ++或Java对象中。

If you do OOP using constructors, prototypes, news, etc, this is something V8 (JS engine in Chrome and Node.js) specially optimizes for. Your user properties will be even stored side by side with their internal properties, like in C++ or Java objects.

用于调整V8的基准之一是面向对象的这个 http://octane-benchmark.googlecode.com/svn/latest/deltablue。 js

One of the benchmarks used to tune V8 is heavily object oriented like this http://octane-benchmark.googlecode.com/svn/latest/deltablue.js

如果你动态附加属性或做一些其他的诡计,你当然会混淆这种启发式方法,可能会让你自己回归。

If you attach properties dynamically or do some other sleazy tricks, you will of course confuse the heuristics for this and probably set yourself back.

这当然是引擎特定的。

我还应该提到一般免责声明,如果你正在做像单击处理程序显示文本区域,那么这无关紧要。您可以在每次方法调用时收集堆栈跟踪,而不会注意任何性能问题。

I should also mention the general disclaimer that if you are doing UI code like "click handler shows an text area", then this doesn't matter. You could be collecting stack traces on every method call and not notice any perf issue.

这篇关于使用带有函数和mixins的对象而不是原型有几个性能损失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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