RMMZ Which approach is faster in the long run?

Drakkonis

Veteran
Veteran
Joined
Mar 5, 2021
Messages
206
Reaction score
156
First Language
English
Primarily Uses
RMMZ
Vague title, I know. Been awhile since I've posted here. Ignore the prefix, the question concerns javascript itself.

I have a bit of code. This bit of code will occur every frame update, hence why I ask which approach is faster.

In this code I have a formula that requires getting a value from a property. Stepping through the code reveals that grabbing this property's value jumps through two other points in the code. There are two of these properties I'm grabbing, and the values are used twice each.

So here's the question. Is it faster to simply grab the property values directly both times, or grab it once to store it in a variable, and then use the variable itself those two times? Is there any real appreciable difference in speed, even if I were to drastically scale up the number of times I used said property value? Or even the number of similar properties I'm accessing each frame?

I don't INTEND to go that massive, but the project I'm working on is a plugin with a great deal of high-degree customization options for the user, so I'd like to know the more time-efficient method just in case someone decides to get.... stupidly creative. :p

There won't be a great deal of such access in any one function, but there isn't a limit to a chain of such functions that a user could theoretically implement.

I've not really delved very far into the topic of time complexity or memory usage, and even cursory inspection on the topics usually implies a LARGE amount of a given operation being done in sequence. Given the frequency with which I'm going to be potentially running one of any number of defined functions, I figured I'd see if there was a definite advantage to one approach over another before I started really getting into writing all those functions.
 

OcRam

Servant of the Universe
Veteran
Joined
Aug 2, 2016
Messages
741
Reaction score
869
First Language
Finnish
Primarily Uses
RMMZ
Vague title, I know. Been awhile since I've posted here. Ignore the prefix, the question concerns javascript itself.

I have a bit of code. This bit of code will occur every frame update, hence why I ask which approach is faster.

In this code I have a formula that requires getting a value from a property. Stepping through the code reveals that grabbing this property's value jumps through two other points in the code. There are two of these properties I'm grabbing, and the values are used twice each.

So here's the question. Is it faster to simply grab the property values directly both times, or grab it once to store it in a variable, and then use the variable itself those two times? Is there any real appreciable difference in speed, even if I were to drastically scale up the number of times I used said property value? Or even the number of similar properties I'm accessing each frame?

I don't INTEND to go that massive, but the project I'm working on is a plugin with a great deal of high-degree customization options for the user, so I'd like to know the more time-efficient method just in case someone decides to get.... stupidly creative. :p

There won't be a great deal of such access in any one function, but there isn't a limit to a chain of such functions that a user could theoretically implement.

I've not really delved very far into the topic of time complexity or memory usage, and even cursory inspection on the topics usually implies a LARGE amount of a given operation being done in sequence. Given the frequency with which I'm going to be potentially running one of any number of defined functions, I figured I'd see if there was a definite advantage to one approach over another before I started really getting into writing all those functions.
Hi,

Properties CAN be SUPER slow - it depends quite a lot of the logic behind said property.

Example in this code below it's almost TWICE slower to read property two times compared to const. And actually logic is really simple... Can't even imagine difference if there would be more complex logic behind the property!
Code:
obj = {}; obj.a = 1;
Object.defineProperty(obj, "something", {
    get: function() {
        return (this.a == 2) ? "It's two" : (this.a > 2) ? "Greater than 2" : "Less than 2";
    }
});

let started = Date.now();
for (let i = 0; i < 100000; i++) {
if (obj.something == "It's two") {
} else if (obj.something == "Anything") {}
}
console.log("Accessed by property:" + (Date.now() - started));

started = Date.now();
for (let i = 0; i < 100000; i++) {
const tmp = obj.something;
if (tmp == "It's two") {
} else if (tmp == "Anything") {}
}
console.log("Accessed by const:" + (Date.now() - started));

EDIT: Single "let" is slightly faster than using const in loop (at least in this case)
Code:
let tmp = "";
for (let i = 0; i < 100000; i++) {
tmp = obj.something;
if (tmp == "It's two") {
} else if (tmp == "Anything") {}
}
 
Last edited:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
33,372
Reaction score
8,395
First Language
German
Primarily Uses
RMMV
I suggest you approach it from a different direction:
how often do the values really change?

if they rarely change it might be better to store the value in a variable with quick access, or only process the calculation after a change instead of going through them every update.

that is how most useless calculations are cut off - not by optimizing them but by only doing them if they are really needed.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,711
Reaction score
2,824
First Language
English
Primarily Uses
RMMZ
As the others have said, this largely depends on what the function you're getting the property from is actually doing.

My general rule of thumb is to reference properties directly if I'm only using them once in a given function, or store them in a shorter identifier if I'm using them twice or more.
 

Drakkonis

Veteran
Veteran
Joined
Mar 5, 2021
Messages
206
Reaction score
156
First Language
English
Primarily Uses
RMMZ
Thanks all for the replies!

In the specific instance I'm working on, the properties I'm grabbing are a window's x and y coordinates. The property is an alias for position.x and position.y, which themselves are linked to the window's transform object/property/whatever it actually is. For the most part, no additional processing is being done, merely jumping through the hoops to get the actual value of the property. I could cut one jump out entirely by using transform.x/transform.y, but I'm using window.x and window.y in the first place to type less code over and over again.

But it's merely one example, I know that grabbing its dimensions are also going to be required. Eventually I might have to grab even more things in that vein, but off the top of my head I'm not 100% sure about that. This evolves more and more as I try to get my original goal with it completed.

In either event, I wasn't sure how speed would be affected with such simple code jumps being repeated. Normally I wouldn't be concerned about it, but as this is a "potentially hundreds of these operations occurring every frame" scenario, I'd rather cut time down on the ground floor, since the potential wait time could grow exponentially, eventually causing enough delay to lag. Not sure how much that would actually take, but I'd rather mitigate that risk as much as possible when/where I can.
 

Latest Threads

Latest Posts

Latest Profile Posts

BCj
Working on Escalia again, feels good. Have this overwhelming urge to map as well, but need to get some extra tiles commissioned first :D
Lee Sang wrote on Bandito's profile.
Hi, I've just tried your plugin "Game is Active" and when I tested it on Android, it gave me an error, something about "Cannot read property 'underfined' of underfined.
With my project, I want the player to have access to all the tools they need to get from New Game to Credits as soon as possible; I want the player's progress to only be impeded by the player not knowing how to use the tools the game provides.
Just a little close up shot...
w2xZocX.jpg
finally drew the last member of "team outcast" for my game. since my game has a lot fighting game elements in it, there's bosses in teams of three like King of Fighters.

Forum statistics

Threads
113,864
Messages
1,078,007
Members
147,951
Latest member
NitrosTropy
Top