对象引用会占用额外的内存吗? [英] Do object references take up extra memory?

查看:362
本文介绍了对象引用会占用额外的内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有以下复杂对象:

Lets say you have the following complex object:

var object1 = .... // (something complexed)

这会占用 x 的内存量JS应用程序。现在假设您有一些其他对象引用 object1

This takes up x amount of memory in your JS application. Now lets say you have some other objects that reference object1:

var otherObject = { something: true, value: 'yes', object: object1 };

var anotherObject = { color: '#FFF', object: object1 };

我将内存量增加了三倍 object1 最初拿起来了吗?或者对 object1 的引用是否不会增加所用内存的开销?

Have I tripled the amount of memory that object1 originally took up? Or do the references to object1 not add to the overhead of the memory used?

我不知道如何自己测试一下,以确定答案。 (如果你可以告诉我如何指向一个有助于对此进行基准测试的工具,那么可以给予奖励。)

I'm not sure how to test this myself in order to determine the answer. (Bonus points if you can tell me how to point me to a tool that helps benchmark this).

推荐答案

对象总是通过通过JavaScript引用(请参阅此热门答案)。当然,指向对象的指针需要一些内存(取决于实现),但是比实际对象要少得多。

Objects are always passed by reference in JavaScript (see this popular answer). Pointer to an object takes some amount of memory (depends on implementation), of course, but much less than the actual object.

这篇关于对象引用会占用额外的内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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