Javascript模块模式内存占用和性能 [英] Javascript Module Pattern Memory Footprint and Performance

查看:81
本文介绍了Javascript模块模式内存占用和性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Javascript模块模式来尝试实现类似C#枚举的功能。我有两种方式,我目前正在考虑实现这个功能,但我不明白一种方式与另一种方式的所有好处或优点。

I am using the Javascript Module Pattern to try and implement C# enumeration-like functionality. I have two ways that I am currently thinking about implementing this functionality but I do not understand all the benefits or advantages of one way versus the other.

这是实现1:

var MyApp = (function (app) {

    // Private Variable
    var enums = {
        ActionStatus: {
            New: 1,
            Open: 2,
            Closed: 3
        }
    };

    // Public Method
    app.getEnum = function (path) {
        var value = enums;            
        var properties = path.split('.');
        for (var i = 0, len = properties.length; i < len; ++i) {
            value = value[properties[i]];
        }
        return value;
    };

    return app;

})(MyApp || {});

// Example usage
var status = MyApp.getEnum("ActionStatus.Open");

现在实施2:

var MyApp = (function (app) {

    // Public Property
    app.Enums = {
        ActionStatus: {
            New: 1,
            Open: 2,
            Closed: 3
        }
    };

    return app;

})(MyApp || {});

// Example usage
var status = MyApp.Enums.ActionStatus.Open;

主要区别在于使用私有变量与公共属性来存储枚举。我认为实现1有点慢,但我不确定将枚举保持为私有会减少内存使用量。谁能解释两者(如果有的话)内存占用和性能的差异?任何其他建议/建议表示赞赏。

The main difference is in using a "private" variable vs a "public" property to store the enums. I would think implementation 1 is a little slower but I was not sure if keeping the enums as "private" reduced the memory usage. Can anyone explain the difference in memory footprint and performance for the two (if any)? Any other suggestions/advice are appreciated.

推荐答案


...但我不确定是否保留枚举为私有减少了内存使用量

...but I was not sure if keeping the enums as "private" reduced the memory usage

相反,如果有的话:你仍然必须拥有枚举对象,你必须有一个访问它的功能。

The opposite, if anything: You still have to have the enums object, and you have to have a function to access it.

就速度而言,我不担心。添加的函数调用不会产生任何真正的区别(我进行了调查它担心使用新的 forEach 等等,甚至在IE6上使用大量慢速JS引擎时,它只是没有' t。)。

In terms of speed, I wouldn't worry about it. The added function call won't make any real difference (I looked into it when worried about using the new forEach and such, and even on IE6 with its massively slow JS engine, it just doesn't matter).

在几年内,你可能能够拥有两全其美的优点:由于ECMAScript5的存在,它们是只读的枚举a href =http://es5.github.com/#x15.2.3.7 =nofollow> Object.defineProperties 功能:

In a couple of years, you'll probably be able to have the best of both worlds: Enums that are read-only, thanks to ECMAScript5's Object.defineProperties feature:

var Enums = Object.defineProperties({}, {
    ActionStatus: {
        value: Object.defineProperties({}, {
            New:    {value: 1},
            Open:   {value: 2},
            Closed: {value: 3}
        })
    }
});

// Usage
var n = Enums.ActionStatus.New; // 1

默认情况下,使用 defineProperties 只读

事实上,如果你添加一个ES5垫片来创建<$,你基本上可以拥有它c $ c> Object.defineProperties 在本机尚未拥有它的浏览器上。 shimmed版本将创建读写属性,因为只有本机支持的版本才能真正创建只读属性,但您现在可以编写代码并知道它可以在现代浏览器中正常工作(大约一半)所有网络冲浪者目前都拥有它们,同时仍在工作,只是缺乏稳健性,而不是现代化的。

In fact, you can basically have that now if you add an ES5 "shim" to create Object.defineProperties on browsers that don't yet have it natively. The "shimmed" version would create read-write properties, since only the natively-supported version can really create read-only properties, but you can write the code now and know that it will work as you like on modern browsers (about half of all web surfers currently have them) while still working, just with less robustness, on less-modern ones.

当然,EMCAScript6可能会采取更进一步的措施,但这仍然是未来的事情。

And of course, EMCAScript6 may take things further, but that's still a future thing.

这篇关于Javascript模块模式内存占用和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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