RMMZ Which approach is faster in the long run?

Drakkonis

Veteran
Veteran
Joined
Mar 5, 2021
Messages
205
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
868
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,368
Reaction score
8,392
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,822
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
205
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

Ami
need a time to not open my MV,still addicted with new vegas

i wonder what addiction effect behind playing new vegas too much,maybe INT & Science gone worse in many days :D
cdjensen94 wrote on Kaliya's profile.
Are you the one who develops Kaliya's Visual Map Enhancements. If so I desperately need some help. I bought the plugin back a few months. I've only started using it recently.
Cause MP Damage instead of HP, Localization Plugin for Translation | RPG Maker News #112

Here in Fla were are seeing crazy covid spikes and some vaccinated getting bad infections. I had a mild fever on Thurs but it went down fast. How is everyone else doing. Hopefully you are all well. :)
Wish I was better at art, specifically that I was faster at it, bc this one NPC took Several Days to complete! It's gonna take So Extremely Long to do all of the art for my game, never mind writing it... Anyways, here's my gorgon, Hopernica Sthyxis!

57b88732c7abbcd84981dacf624a98495af61fcc.gifv

Forum statistics

Threads
113,826
Messages
1,077,738
Members
147,883
Latest member
Senppai90
Top