Javascript中最快的类/对象访问/实例化是什么 [英] What is the fastest class/object access/instantiation in Javascript

查看:53
本文介绍了Javascript中最快的类/对象访问/实例化是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在探索构建类的几种不同方法时,我很好奇实例化和访问类的最快方法是什么。基于另一个问题的输入:

While exploring a few different ways to build classes, I am curious as to what is the fastest way to instantiate and access classes. Based on input from another question:

Javascript范围和self = this,_this = this,that = this

我现在正在研究三种方法创建一个类并且好奇有什么输入用于创建和访问对象的最快方法。

I am now looking at three methods for creating a class and curious what input is out there for the fastest way to create and access objects.

var DogClass = function DogClass(_age) {
    this.age=_age;

    this.setAge = function(num) {
        this.age=num;
    }.bind(this);    

    this.getAge = function() {
        return this.age;
    }.bind(this);    
};

var BirdClass = function BirdClass(_age) {
    var _this=this;
    this.age=_age;

    this.setAge = function(num) {
        _this.age=num;
    };    

    this.getAge = function() {
        return _this.age;
    };    
};

var CatClass = function CatClass(_age) {
    this.age=_age;
};

CatClass.prototype.setAge = function (num) {
    this.age=num;
};
CatClass.prototype.getAge = function () {
    return this.age;
};

function profileCreate() {
    console.log("Creating a million of each");

    var o,i,iterations=1000000;

    console.time('createDog');
    for(i=0;i<iterations;i++) {
        o=new DogClass(4);    
    }
    console.timeEnd('createDog');

    console.time('createCat');
    for(i=0;i<iterations;i++) {
        o=new CatClass(4);    
    }
    console.timeEnd('createCat');

    console.time('createBird');
    for(i=0;i<iterations;i++) {
        o=new BirdClass(4);    
    }
    console.timeEnd('createBird');
}

function profileAccess() {
    console.log("Accessing a million of each");

    var o,i,iterations=1000000;

    console.time('accessDog');
    for(i=0;i<iterations;i++) {
        fido.setAge(5);
    }
    console.timeEnd('accessDog');

    console.time('accessCat');
    for(i=0;i<iterations;i++) {
        fluffy.setAge(6);
    }
    console.timeEnd('accessCat');

    console.time('accessBird');
    for(i=0;i<iterations;i++) {
        tweety.setAge(7);
    }
    console.timeEnd('accessBird');
}

创造一百万美元


  • createDog:1531.654ms

  • createCat:7.301ms

  • createBird:605.982ms

获取每个百万美元


  • accessDog:197.338ms

  • accessCat:11.404ms

  • accessBird:7.031ms

如果我第二次选择配置文件访问按钮,则accessCat方法要快得多。

If I select the "profile access" button a second time, the accessCat method is much faster.


  • accessDog:185.607ms

  • accessCat:0.958ms

  • accessBird:11.095ms

???由于传递静态数字,解释器是否优化了调用?

??? Is the interpreter optimizing the calls due to a static number being passed?

这是小提琴: http://jsfiddle.net/sday/yrropeer/3/

关于我可能犯的错误的任何想法在代码中有无效的迷你性能测试?在我看来,原型是创建类最快的,而BirdClass是访问速度最快的。

Any thoughts on what errors I might have in the code to invalidate this mini performance test? Seems to me the prototype is the fastest for creating classes, and the BirdClass is the fastest for access.

推荐答案

有很多设计模式,以创建类,对象和每个有自己的优点和缺点。为了比较它们,请查看

There are many design patterns in order to create class, objects and each one has its own pros ans cons. in order to compare them look at this.

这篇关于Javascript中最快的类/对象访问/实例化是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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