简体   繁体   English

Javascript原型与一般功能 - 性能/可读性

[英]Javascript Prototype vs General Functions - Performance/Readability

So I wrote these tests to see how much faster using prototypes would be... 所以我写了这些测试,看看使用原型会有多快...

function User() {
    return {
        name: "Dave",
        setName: function(n) {
            this.name = n;
        },
        getName: function() {
            return this.name;
        }
    };
}

function UserPrototype() {
    if (!(this instanceof UserPrototype)) return new UserPrototype();
    this.name = "Dave";
}
UserPrototype.prototype.getName = function() {
    return this.name;
};
UserPrototype.prototype.setName = function(n) {
    this.name = n;
};

function setName(obj,name)
{
    obj.name = name;
}
function getName(obj)
{
    return obj.name;
}

//Test 1
var c = 10000000;
var tstart = 0;
var tend = 0;
tstart = new Date().getTime();
for (var j = 0; j < c; j++) {
    var newUser = User();
    newUser.setName("michael");
    newUser.getName();
}
tend = new Date().getTime() - tstart;
console.log("Returning object with methods: " + tend / 1000.0 + " seconds");
//Test 2
tstart = new Date().getTime();
for (var j = 0; j < c; j++) {
    var newUser = new UserPrototype();
    newUser.setName("michael");
    newUser.getName();
}
tend = new Date().getTime() - tstart;
console.log("Using prototypes: " + tend / 1000.0 + " seconds");
//Test 3
tstart = new Date().getTime();
for (var j = 0; j < c; j++) {
    var newUser = {name:"dave"};
    setName(newUser,"michael");
    getName(newUser);
}
tend = new Date().getTime() - tstart;
console.log("Using general functions: " + tend / 1000.0 + " seconds");
​

My results: 我的结果:

Returning object with methods: 9.075 seconds
Using prototypes: 0.149 seconds 
Using general functions: 0.099 seconds 

I wrote the first two tests and when I saw the results I thought about why I was seeing them... I'm thinking the reason is that the object returning is slow due to the fact that two new method property instances are created every time the object is instantiated while the prototype method is faster because it just creates the function once. 我写了前两个测试,当我看到结果时,我想到了为什么我看到它们......我在想,原因是返回的对象很慢,因为每次都会创建两个新的方法属性实例在原型方法更快的情况下实例化对象,因为它只创建一次函数。 The closeness of performance between general function calls and prototypes makes me think I'm right about my assumption. 一般函数调用和原型之间的性能接近让我觉得我的假设是正确的。

So my first question is, am I right about my assumption? 所以我的第一个问题是,我对我的假设是对的吗?

My second question is, how can I make writing with prototypes more readable but with keeping the high performance? 我的第二个问题是,如何使原型的写作更具可读性,同时保持高性能? Is there a way to code prototypes in a way that would look like they are in a "class" (if that makes sense) 有没有办法以一种看起来像是在“类”中的方式对原型进行编码(如果这样做有意义的话)

*EDIT - I forgot to do a test with Object.create(), just did one and posted results. *编辑 - 我忘了用Object.create()做测试,只做了一个并发布了结果。 JSFiddle: ( http://jsfiddle.net/k2xl/SLVLx/ ). JSFiddle:( http://jsfiddle.net/k2xl/SLVLx/ )。

I get now : 我现在得到:

Returning object with methods: 0.135 seconds fiddle.jshell.net:63
Using prototypes: 0.003 seconds fiddle.jshell.net:72
Using general functions: 0.002 seconds fiddle.jshell.net:81
Returning object.create version: 0.024 seconds 

Looks like this might be the solution? 看起来这可能是解决方案?

I agree with your assumption. 我同意你的假设。 This is also bourn out if the code is written like this: 如果代码编写如下:

function UserObject() {
    this.name = "Dave";

    this.getName = function() {
        return this.name;
    };
    this.setName = function(n) {
        this.name = n;
    };
}

In this case, like in your "object" method, the getName and setName methods are created every time a UserObject object is constructed. 在这种情况下,就像在“对象”方法中一样,每次构造UserObject对象时都会创建getNamesetName方法。

There are slight differences in your code between the "prototypes" and "functions" methods. 您的代码在“原型”和“函数”方法之间存在细微差别。 Removing if (!(this instanceof UserPrototype)) return new UserPrototype(); 删除if (!(this instanceof UserPrototype)) return new UserPrototype(); (which is an unnecessary safe guard) shaves quite a bit off. (这是一个不必要的安全防护)剃掉了相当多的东西。 Arguably too, a closer comparasion to ... 可以说,与......进行更密切的比较

var newUser = new UserPrototype();
[newUser].name = "Dave";

... is ... ......是......

var newUser = new Object();
newUser.name = "dave";

... because ... ......因为......

var newUser = {name:"dave"};

... rides high on native code when creating the Object and adding the name property. ...在创建Object并添加name属性时,在本机代码上骑得很高。

In that case, the results flip and the "prototypes" methods comes out faster. 在这种情况下,结果翻转,“原型”方法更快。 See a jsFiddle here: 在这里查看jsFiddle:

About how to make your prototypes more readable, I can't help. 关于如何使您的原型更具可读性,我无能为力。 To me, they are readable :-) 对我来说,它们是可读的:-)

When you're not using a this value inside the constructor, never ever use it in the constructor but as a prototype. 当你在构造函数中没有使用这个值时,永远不要在构造函数中使用它,而是作为原型使用它。 Eg 例如

UserPrototype.prototype.name = "Dave"; UserPrototype.prototype.name =“Dave”;

When you do that, prototype wins in speed. 当你这样做时,原型在速度上获胜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM