Which is the better practice and why?
Declare a variable:
exampleFunction(requestData: Object) {
const username = requestData.username;
doSomething(username);
}
Or access the object property directly?
exampleFunction(requestData: Object) {
doSomething(requestData.username);
}
What if the code I use is over 50 lines long and the variable is used multiple times. Should I use the variable 'username' multiple times or use 'requestData.username' multiple times?
Disadvantages of closures There are two main disadvantages of overusing closures: The variables declared inside a closure are not garbage collected. Too many closures can slow down your application. This is actually caused by duplication of code in the memory.
Closures are frequently used in JavaScript for object data privacy, in event handlers and callback functions, and in partial applications, currying, and other functional programming patterns.
To access the object, a method can use the this keyword. The value of this is the object “before dot”, the one used to call the method.
Closures are useful because they let you associate data (the lexical environment) with a function that operates on that data. This has obvious parallels to object-oriented programming, where objects allow you to associate data (the object's properties) with one or more methods.
Declaring an object variable as a specific class gives you several advantages: When Option Strict is turned On, an object variable can access only the methods and properties of the class with which you declare it. The following example illustrates this.
You can declare an object variable with the Object data type when the specific object type is not known until the procedure runs. Use the Object data type to create a generic reference to any object. If you know the specific object type, you should declare the object variable as that object type.
You can treat an object variable exactly the same as the object to which it refers. You can set or return the properties of the object or use any of its methods. Declare the object variable.
How to: Declare an Object Variable and Assign an Object to It in Visual Basic. You declare a variable of the Object Data Type by specifying As Object in a Dim Statement. You assign an object to such a variable by placing the object after the equal sign (=) in an assignment statement or initialization clause.
V8 Javascript Engine performs some optimization pre-processing the code and checking if some variable can be deleted or if there's some regular pattern in the code, in order to reduce the execution payload.
For this reason, there are no memory issues in using a form or another, at least using V8 engine (for example, Chrome or NodeJS).
Even if this optimization isn't performed, it's just a pointer or a primitive variable, so it won't cost more than few bytes.
It's different if we talk about the cost of transmitting code. We know that JavaScript isn't a compiled language and it needs to reach the client's computer to be executed, so every space, indentation, semicolon, will cost in order of time used to transfer.
To reduce this cost, you can uglify and minify your code, but usually this type of optimization isn't performed.
Finally, it's impossible to think a scenario where it can make a difference, so I advise you to use the form that you think it's more readable and no matter of other parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With