简体   繁体   English

函数和对象之间的差异,以及它如何影响性能

[英]Differences between functions and objects, and how it affects performance

Could anyone shred some light on why there seems to be such a great difference in memory footprint between the two examples shown below? 任何人都可以解释为什么下面显示的两个例子之间的内存占用似乎有如此大的差异? I'm using node.js as environment. 我正在使用node.js作为环境。

They both produce the same result and mapping 它们都产生相同的结果和映射

var util = require('util');

var startMem = process.memoryUsage().heapUsed;

/*
var test = function() {

    var testvar = function() {
        this.innerTest1 = function() {
            this.someVal1 = "this is a test";
            this.someFunc1 = function() {
                return "this is another test";
            }
        }
        this.innerTest2 = function() {
            this.someVal2 = "this is a third test";
            this.someFunc2 = function() {
                return "this is a forth test";
            }
        }
    }
}
*/
//out:
//mem: 448088


var test = function() {
    var testvar = {
        innerTest1 : {
            someVal1 : "this is a test",
            someFunc1 : function() {
                return "this is another test"
            }
        },
        innerTest2 : {
            someVal2 : "this is a third test",
            someFunc2 : function() {
                return "this is a forth test"
            }
        }
    }
}

for (var i = 0; i < 50000; i++) {
    var testobj = new test();
}

//out:
//mem: 1060040


console.log("mem: " + (process.memoryUsage().heapUsed - startMem));

The two snippets of code have entirely different behavior. 这两段代码具有完全不同的行为。

In the first example, the properties/functions are not set or created until the containing functions are invoked. 在第一个示例中,在调用包含函数之前, 不会设置或创建属性/函数。 However, the containing functions are never invoked. 但是, 永远不会调用包含函数。

For instance, given: 例如,给定:

   this.innerTest1 = function() {
        // the property is NOT SET until this function is run
        this.someVal1 = "this is a test";
        // this function is NOT CREATED until this function is run
        this.someFunc1 = function() {
            return "this is another test";
        }
    }

Likewise, the same applies for assigning to innerTest1 . 同样,同样适用于赋值给innerTest1 Thus, the first example code is equivalent the following which is not very useful. 因此,第一个示例代码等同于以下不太有用的代码。

var testvar = function() {}

(The parsing can be considered a one-time cost.) (解析可以被认为是一次性费用。)

In the later example, every property is assigned and every function is created as part of the object literal evaluation. 在后面的示例中,将分配每个属性,并且每个函数都是作为对象文字评估的一部分创建的。

In the first example functions are getting created lazily only when their parent function is called, but in the second example all members are initialized as part of the object literal. 在第一个示例中,仅在调用其父函数时才会延迟创建函数,但在第二个示例中,所有成员都初始化为对象文字的一部分。

If you are trying to reduce your memory footprint , you should make use of the prototype chain which will allow you to share functions between multiple instances. 如果您正在尝试减少内存占用 ,则应该使用原型链,这将允许您在多个实例之间共享功能。

Basically, rather than doing: 基本上,而不是做:

function test() {
    var testvar = {
        someFn: function () {}
    };
}

You can do: 你可以做:

var base = {
    someFn: function () {}
};

function test() {
    var testvar = Object.create(base);
}

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

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