简体   繁体   English

Node.JS表现异常

[英]Node.JS behaves strange

I have a variable called uids 我有一个名为uids的变量

var uids = [];

Then I write some value to it property 然后我给它写一些值属性

uids[16778923] = "3fd6335d-b0e4-4d77-b304-d30c651ed509"

But before it 但在此之前

if (!uids[user.id]) {
 uids[user.id] = generateKey(user);
}

This thing behaves ok. 这东西表现不错。 If I try to get the value of it property 如果我尝试获取it属性的值

uids[currentUser.id]

It will give me a value of this property. 它会给我这个属性的价值。 If I try to call some methods like 如果我尝试调用某些方法,例如

Object.keys(uids);

It will give me, what I expected. 它会给我我所期望的。 And here the mystery comes... 神秘来了...

uids;

RAM rest in piece. RAM碎片。 See the node eating ram 看节点吃公羊

I am very confused now. 我现在很困惑。 What's wrong? 怎么了?

This is because you are creating a huge array and node will reserve memory for it - who knows what comes. 这是因为您正在创建一个巨大的数组,节点将为其保留内存-谁知道会发生什么。 I'd say that's a scenario where you would use a Map (or a plain object, but Map feels better here. 我想说的是您会使用Map (或普通对象)的情况,但是Map在这里感觉更好。

var uids = new Map();
var key = 456464564564654;
if (! uids.has(key)) {
    uids.set(key, generateKey(user))
}

You are creating an empty array (length is zero), then you assign some value to an arbitrary index. 您正在创建一个空数组(长度为零),然后将一些值分配给任意索引。 This will make the array grow as big as the index and assign the value to that index. 这将使数组增长到与索引一样大,并将值分配给该索引。 Look at this example using node.js REPL: 使用node.js REPL查看此示例:

> var a = []
undefined
> a[5] = "something"
'something'
> a
[ , , , , , 'something' ]
> a.length
6

Instead of creating an array, you could create a Map() or an common javascript object (singleton). 除了创建数组之外,您还可以创建Map()或常见的javascript对象(单例)。 Javascript objects behave like Maps but only Strings can be used as keys. Javascript对象的行为类似于Maps,但只有字符串可以用作键。 If you assign a Number to be key, javascript will convert it to String automatically. 如果您将数字指定为键,则javascript会自动将其转换为字符串。

Personally, I would go with objects because they perform better. 就个人而言,我会选择对象,因为它们的性能更好。 Instantiating an object takes longer than instantiating a Map (and it doesn't seem like you need to create several groups of "uids"), but once done, adding new keys and retrieving values from any key in faster when using common objects. 实例化一个对象要比实例化一个Map花更长的时间(似乎不需要创建几组“ uid”),但是一旦完成,添加新键并在使用公共对象时可以更快地从任何键中检索值。 At least that's how things go in my node.js v6.7.0 on ubuntu 14.04 but you could try for yourself. 至少这就是我在ubuntu 14.04上的node.js v6.7.0中运行的方式,但是您可以自己尝试。 And it would also make the least alteration to your code. 而且这也将对您的代码进行最少的改动。

var uids = {} // common/ordinary empty javascript object instead of array.
if (!uids[user.id]) { // getting value from one key works the same.
    uids[user.id] = generateKey(user) // assignment works the same.
}
////
uids[16778923] = "3fd6335d-b0e4-4d77-b304-d30c651ed509" // key will be "16778923".
uids[16778923] // getting value for key "16778923" can be done using 16778923 instead of "16778923".
////
uids[currentUser.id] // still returning values like this.
Object.keys(uids) // still returning an array of keys like this. but they are all Strings.

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

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