简体   繁体   English

Javascript模块模式内存占用和性能

[英]Javascript Module Pattern Memory Footprint and Performance

I am using the Javascript Module Pattern to try and implement C# enumeration-like functionality. 我正在使用Javascript模块模式来尝试实现类似C#枚举的功能。 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. 我有两种方式,我目前正在考虑实现此功能,但我不明白一种方式与另一种方式的所有好处或优点。

Here is implementation 1: 这是实现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");

And now implementation 2: 现在实施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;

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. 我认为实现1有点慢,但我不确定将枚举保持为“私有”会减少内存使用量。 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. 相反,如果有的话:您仍然必须拥有枚举对象, 并且您必须具有访问它的功能。

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). 增加的函数调用不会做出任何真正的区别(我看着它的时候担心使用新forEach和这样的,甚至在IE6凭借其大量慢的JS引擎,它只是无所谓)。

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: 在几年内,您可能能够拥有两全其美的Object.defineProperties :由于ECMAScript5的Object.defineProperties功能,它们是只读的枚举:

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

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

By default, properties created with defineProperties are read-only . 默认情况下,使用defineProperties创建的属性是只读的

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. 实际上,如果你添加一个ES5“shim”来创建Object.defineProperties ,而这些浏览器还没有它本身存在,你基本上可以拥有它。 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. “shimmed”版本将创建读写属性,因为只有本机支持的版本才能真正创建只读属性,但您现在可以编写代码并知道它可以在现代浏览器中正常工作(大约一半)所有网络冲浪者目前都拥有它们,同时仍在工作,只是缺乏稳健性,而不是现代化的。

And of course, EMCAScript6 may take things further, but that's still a future thing. 当然,EMCAScript6可能会更进一步,但这仍然是未来的事情。

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

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