You are reading a single comment by @Sacha and its replies. Click here to read the full conversation.
  • Hi Espruinos,

    I just searching a proper way to detect if a variable is defined/exists without loosing memory. Yes it's all about memory....

    I tried it in two ways:

     if(variable !==undefined)
    

    and

    if(typeof variable !=='undefined')
    

    Here is the memory loosing code:

    test={};
    
    console.log(process.memory());
    for(i=0;i<1000;i++) {
      if(test['hello'+i]!==undefined)
      {
        console.log('hello'+i+' is defined');
      }
    }
    console.log(process.memory());
    
    Output:
    
    {"free":1780,"usage":20,"total":1800,"history":5,"stackEndAddress":536909512,"flash_start":134217728,"flash_binary_end":134432224,"flash_code_start":134443008,"flash_length":262144}
    {"free":778,"usage":1022,"total":1800,"history":18,"stackEndAddress":536909512,"flash_start":134217728,"flash_binary_end":134432224,"flash_code_start":134443008,"flash_length":262144}
    

    Each test if the variable exists creates the variable as undefined and uses memory.
    You will see it when you output the array like this:

    console.log(test);
    

    Output:

    {"hello0":undefined,"hello1":undefined,"hello2":undefined,"hello3"...........
    

    Many thanks for your help

    Sacha

About

Avatar for Sacha @Sacha started