WildeMe WildeMe - 1 year ago 57
Node.js Question

Node.JS behaves strange

I have a variable called 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 will give me a value of this property. If I try to call some methods like


It will give me, what I expected. And here the mystery comes...


RAM rest in piece. See the node eating ram

I am very confused now. What's wrong?

Answer Source

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.

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