I am learning Javascript currently.I was wondering if there is any difference between:
var factor=0.1;
var limit=10;
var x;
var y;
x= limit*factor;
y= limit*factor;
//or
var limit=10;
var x;
var y;
x=limit *0.1;
y=limit*0.1;
Does it make any difference (when looking at performance for example)? If so, why it is different? The second example looks less promising to me, because I keep thinking that I am declaring the variable 0.1 twice. Thanks for your help in advance.
-
in second example you are not declaring "0.1" twice and second would be more performant.binariedMe– binariedMe2017年05月07日 10:32:44 +00:00Commented May 7, 2017 at 10:32
-
Do not start with optimization.Aluan Haddad– Aluan Haddad2017年05月07日 10:32:47 +00:00Commented May 7, 2017 at 10:32
-
1If you change 0.1 to 0.2 you will need to update it in all places where you use it. So the first version is better.dfsq– dfsq2017年05月07日 10:32:48 +00:00Commented May 7, 2017 at 10:32
-
inline function, variable are always more performant but less reusablebinariedMe– binariedMe2017年05月07日 10:33:22 +00:00Commented May 7, 2017 at 10:33
-
3No, there's no difference. The JavaScript engine should optimize both versions. Also, if there would be any difference it can safely be ignored. Just go for the one that is more readable.XCS– XCS2017年05月07日 10:36:00 +00:00Commented May 7, 2017 at 10:36
2 Answers 2
There is a very small difference. When you use factor in the two multiplications, the JavaScript engine has to go look up the value of factor in the current lexical environment object each time — in theory, anyway; optimization may well be able to get rid of that, if and when the code is chosen for optimization by the JavaScript engine.
But regardless: Worry about performance problems when you have a performance problem to worry about. Instead, concentrate on readability and maintainability. If x and y are meant to be multiplied by the same value, put that value in something (a var, or perhaps a const with ES2015+), and use it in both places.
5 Comments
0.1 vs. factor. I doubt there's any real difference. But the code only gets parsed once when loaded, after which the parsed result is either interpreted or compiled. So that parsing difference, if there is one, isn't going to matter, whereas the symbol lookup could. (Just a few months ago I would have said that virtually no JavaScript engine uses an interpreter anymore, but recently V8 went back to interpretation -- bytecode-level, like Java -- for code that's only used rarely.)I would suggest you go ahead with the first example, but with a modification. Variables are meant to hold dynamic data, it is better to hold 0.1 in a variable, so you can change it over time if required.
// have a function so that you don't repeat the code
function getFactor(factor, limit) {
return limit * factor;
}
//declare the variables and set the required default values
var factor = 0.1,
limit = 10,
x, y;
//assign getFactor to x and y variables
x = y = getFactor(factor, limit);
3 Comments
var.Explore related questions
See similar questions with these tags.