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
@Sacha started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
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:
and
Here is the memory loosing code:
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:
Output:
Many thanks for your help
Sacha