Performance problem when declaring variables

Asked

Viewed 77 times

1

I’m making a code that a very large function, where I declare some local variables with const and let. To improve his reading I’m dividing this function into several small functions. So I ended up declaring within the scope of these functions, the variables again. What used to be, for example, 5 local variables have now multiplied into 50 or more local variables within these functions.

What’s best?

1 - Declare these variables with let and const only once outside the scope of the functions. And then go only by assigning new values to these variables within the functions (except for const).

let thisHelp = null;
let thisSymbol = null;
let thisText = null;

function myFunction() {
  thisHelp = SceneManager._scene._helpWindow;
  thisSymbol = SceneManager._scene._categoryWindow.currentSymbol();
  thisText = Eli.Param.HelpWindows.menuText
  thisHelp.x = 100;
};

function myFuntion2() {
  thisHelp = SceneManager._scene._helpWindow;
  thisSymbol = SceneManager._scene._categoryWindow.currentSymbol();
  thisText = Eli.Param.HelpWindows.etc;
  thisHelp.x = 100;
};

2 - Do the same, declare and assign a value to variables only within the functions.

function myFunction() {
  const thisHelp = SceneManager._scene._helpWindow;
  const thisSymbol = SceneManager._scene._categoryWindow.currentSymbol();
  const thisText = Eli.Param.HelpWindows.menuText
  thisHelp.x = 100;
};

function myFuntion2() {
  const thisHelp = SceneManager._scene._helpWindow;
  const thisSymbol = SceneManager._scene._categoryWindow.currentSymbol();
  const thisText = Eli.Param.HelpWindows.etc;
  thisHelp.x = 100;
}

3 - I’m worried a lot and this for Javascript is not a question of performance. I mean, I can do it anyway.

  • 1

    If you want to check the performance between two alternatives, where the result is not obvious, measure the time taken to execute each option (v.g use console.time() and console.timeend()). Apart from that, the problem with your example is that people who work with you or depend on your code will look at the first case, see three global variables without a clear need and already go to be very sorry.

  • 1

    Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

2 answers

2

It may be that the code posted is not the same one you are using, but in this case I didn’t see much real difference in making one way or another from the point of view of typing or readability. This code does not deal with the object defined in a class, which would be different there. I can’t talk about something that might have been if it had been done right because the question is about this code like this. I don’t know if this is u mcaso to create a class (or prototype).

Actually, I only see disadvantages in the first case. In addition to using state with global scope that is usually bad is less readable mainly in large code because the variable declaration does not stay where it is being used. Worse still be required to make it variable when the other way can be constant.

In scripts global scope is not usually a problem, but today people use JS to make applications, there weighs.

The question says not to worry about performance, but puts the tag of this matter, then you worry and you don’t even know. The global state has worse performance, even if the difference is insignificant and there is no way the compiler, still an interpreted language make an optimization of this type.

The name pattern adopted is very strange and can cause confusion.

The whole problem must have started when tried to divide the function into several, this made losing the cohesion and caused a problem that Ora is trying to fix, but wouldn’t it be better to fix the real problem that didn’t exist before, so go back to what it was? Probably read that should divide into small functions and now is trying to apply as if it were a cake recipe.

A lot of people who work in the field don’t really know how to program because they follow cake recipe, it’s important you understand the whys of every thing they invent out there, so it can be critical and determine when to use something and when to avoid it. A lot of the things that people say it’s to do only applies in some cases, but many learn it should be at all.

On the other hand what was posted may give wrong indications of what you are actually doing, then my answer would be another.

-1


There is no problem of performance you make designations. Probably even compilers optimize this.

Okay, kicking an answer, more related to programming patterns in general.

It is very common for a set of functions to share the same context or state. There are several ways you can share the same context across multiple functions without code repetition. Which one is best? I don’t know, it’s very much a matter of taste.

Grouping

If you have a set of common variables, you can group them into one object. And then you pass this object as parameter to several functions.

fn1(estado, a, b, c)
fn2(estado, 1, 2)
fn3(estado, x, y)

Classes

A class is a state container. Functions (methods) of a class share the same variables. We would write the above code as follows with classes:

const grupo = new MyClass(estado)
grupo.fn1(a, b, c)
grupo.fn2(1, 2)
grupo.fn3(x, y)

Closures

Functions are also state containers due to the way the so-called "lexical scope" works. The use of a state keeping function is commonly called "closure". It is a slightly different way of writing to achieve a result similar to the use of classes.

function MeuGrupo(estado) {
  return {
    fn1(a, b, c) { ... },
    fn2(a, b) { ... },
    fn3(x, y) { ... }
  }
}

const grupo = MeuGrupo(estado)
grupo.fn1(a,b,c)
grupo.fn2(1, 2)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.