Homeyscript - const or var?

Is there any real difference between using const and var (except for the obvious - one being constant and the other being variable) in homeyscript?

Looks like that it is a real difference for me, constant or variable?

Nice explanation here: JavaScript Variables

1 Like

var is mostly deprecated, only in very specific circumstances should you use them. Nowadays either use const or let.

1 Like

A constant cannot be altered after setting.
A var is changable, but defines the variable outside the direct scope, inside the function scope.
A let is changeable, but defines a variable inside the scope.

See the answer/solution for the question here (because you might misuse var/let):


Thanks! As let was new to me, the scoping was interesting.

Will using let also help with memory allocation? I guess it’s destroyed once out of scope?

(More simpler answer, yes the let itself is destroyed at the end of the current scope/block.)

Well, thats quite hard to explain, it depends on:

  • What type is placed within the var/let.
  • Which environment is is run in.
  • How it is being referenced.

By variable i mean a let variable also in this post!

In most environement, for type number and boolean: Yes, it will be detroyed asap (during Garbage collection).
Please understand that most (if not all, not sure) JavaScript engines are using variables always as reference: you are not really settings the variable. So if you set letA to 1, it actually creates a “hidden” memory part for the 1, and the letA references that 1. So if you set the letA to 2, the 1 still exists in memory, until garbage collection notices that the 1 is not referenced anymore, and removes it.
So, clearing variables to empty values, is about the same as not referencing it in your code anymore, hardly no point in settings letA to undefined, unless you wanna use that variableNAME later on again.

However, for strings, it’s another story, especially for the (Google’s) V8 engine.

Lets say you place a large text in let1, about 100 charactes (not realy large i know, but enough to make the point).

Now, you take a substring of more than 12 characters (so, 13 or more) and place it in a let2, the let2 is actually a (sub/partial) reference to let1.
So, even tho you might not use and reference the let1 anywhere in the continueing code, the whole let1 will be kept in memory and is not garbage collected.
So, be very carefull with strings and substrings, longer than 12 characters.
Ofc, this is very speed and memory effecient when using and manipulating large text, when the substrings are also large.

However, it can also create massive memory leaks if you do not understand that part correctly.
Example: Do not read complete websites, just to take the first 13 characters, because during the process and garbage collection, it will keep the whole website text, it will not see if it’s just a part and remove the rest.

Ofc, if you than not use the variables anymore, the text is removed from memory.
But if you use scopes the way Angular(JS) uses in browsers, and append multiple 13-char-substring from differend large texts, and place the substring in the angular scope, you will notice massive memory increases, and it will not reduce at all.

I use a custom substring function for cases as these…

Hang on…


Here, this substring function, will make sure the new string is a completly new string.
It will create a lott of strings in memory, but as soon as the garbage collection is done, only the resulting string is kept.
If you do not reference the original string yourself anymore, even that one is deleted.
It’s less performing (speedwise) as the normal substring when using it ofc, but it can, when used correctly, keep memory very low, making a lott of systems run faster in the end.

Only use this when all below point are true:

  • When the original string is very large (depends on the moment and vision i guess).
  • When the substring is 13 characters or more
  • The JavaScript engine uses the “string mirror/reflection” (okay, the non-reflection is string slicing).

So, another example: For AVD/TFE templates, i read the Forum, and i only need a few parts, one is the urls, which are mostly longer that 12 characters
For that purpose, this script reduced the memory extremly, keeping the app running much faster.

function substring(str, start, end) {
    return !str ? str : str.substring(start, end);
    if (!str) return str;
    let s = '', d, _end = (end || str.length);
    while (start < _end) {
        s += str.substring(start, start + (d = (d = _end - start) > 12 ? 12 : d));
        start += d;
    return s;

A bit more information on the 12 char nostring_slicing (can be configured in some cases):

Not sure which parts are fixed or improved, but i did a lott of test for SimpleLog a few months back, and the used Node/Engine/Settings did use “substring Mirror/Reflect” way of substrings.
So be carefull with substrings from large texts on HP :wink: